如何使用jQuery隐藏除特定一个之外的所有子div?

时间:2022-11-25 18:10:28
<div id="target">
    <div id="exclude"></div>
    <div></div>
    ...
</div>

$('#target').children().hide(); will hide all.

$( '#目标')儿童()隐藏()。将隐藏所有。

5 个解决方案

#1


50  

What you're wanting to do is hide all of the siblings of a particular element. That's relatively simple with jQuery using the .siblings method:

你想要做的是隐藏特定元素的所有兄弟姐妹。使用.siblings方法的jQuery相对简单:

​$("#exclude").siblings().hide();​​​​

This will hide all elements on the same level, in the same parent element.

这将隐藏同一级别中同一父元素中的所有元素。

#2


8  

I believe that $('#target > div').not('#exclude').hide() should do what you want.

我相信$('#target> div')。not('#exclude')。hide()应该做你想要的。

Or alternately if you want sub-children that are divs too, $('#target div').not('#exclude').hide()

或者,如果你想要也是div的子孩子,$('#target div')。not('#exclude')。hide()

#3


2  

$('#target').children().hide();
$('#exclude').show();

#4


1  

Use the :not selector

使用:not selector

#5


1  

Have you tried using the "not" selector with the id that you want to exclude?

您是否尝试将“not”选择器与要排除的ID一起使用?

http://docs.jquery.com/Selectors/not#selector

http://docs.jquery.com/Selectors/not#selector

Also, the obvious answer would be to follow it with a $('#exclude').show()

另外,明显的答案是用$('#exclude')跟随它.show()

#1


50  

What you're wanting to do is hide all of the siblings of a particular element. That's relatively simple with jQuery using the .siblings method:

你想要做的是隐藏特定元素的所有兄弟姐妹。使用.siblings方法的jQuery相对简单:

​$("#exclude").siblings().hide();​​​​

This will hide all elements on the same level, in the same parent element.

这将隐藏同一级别中同一父元素中的所有元素。

#2


8  

I believe that $('#target > div').not('#exclude').hide() should do what you want.

我相信$('#target> div')。not('#exclude')。hide()应该做你想要的。

Or alternately if you want sub-children that are divs too, $('#target div').not('#exclude').hide()

或者,如果你想要也是div的子孩子,$('#target div')。not('#exclude')。hide()

#3


2  

$('#target').children().hide();
$('#exclude').show();

#4


1  

Use the :not selector

使用:not selector

#5


1  

Have you tried using the "not" selector with the id that you want to exclude?

您是否尝试将“not”选择器与要排除的ID一起使用?

http://docs.jquery.com/Selectors/not#selector

http://docs.jquery.com/Selectors/not#selector

Also, the obvious answer would be to follow it with a $('#exclude').show()

另外,明显的答案是用$('#exclude')跟随它.show()