undefinedfix
Sign in

JQuery can't make an element after clicking on it display:none ?

charlesw edited in Thu, 21 Oct 2021
<div id="xiaoshi"> 
    <div></div>//想点击这个元素后给最外层div设置display:none,
    <div id="div3"></div>//出来的效果却是只消失了div3和div4,最外层div和点击的都在
    <div id="div4"></div>
</div>

<div class="我是外面的div"></div>
//但是代码应该没问题,因为点击这个外面的div可以让上面四个div都消失
5 Replies
openHBP
commented on Thu, 21 Oct 2021

http://runjs.cn/detail/ujrejdju I don't know about yours. Mine is OK. Let's see your JS

yangmole
commented on Thu, 21 Oct 2021

$('#xiaoshi>div').eq(0).onclick(function(){ $('#xiaoshi').hide()})

fudxe
commented on Fri, 22 Oct 2021

You use siblings to find elements. You should use parent

Smith00101010
commented on Fri, 22 Oct 2021

$("#xiaoshi").on("click"," div:eq (0)",function(){

//$(this).parents("#xiaoshi").css('display', 'none');
$("#xiaoshi").css('display', 'none');

}) in fact, the main thing is to get familiar with the DOM operation of JQ first, and then to understand the events of JQ. Maybe it's because you wrote something less or you wrote something wrong. There's no problem with either. You can try it.

villau
commented on Fri, 22 Oct 2021

$(document).ready(function() {

$("#xiaoshi div").click(function() {
    $(this).parent().css("display", "none");
});

})

lock This question has been locked and the reply function has been disabled.