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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2025617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:36:55  来源:igfitidea点击:

How to hide all children div except a specific one with jQuery?

jquery

提问by user198729

<div id="target">
    <div id="exclude"></div>
    <div></div>
    ...
</div>

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

$('#target').children().hide();将隐藏所有。

回答by Sampson

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

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

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

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

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

回答by Crast

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()

回答by Toki

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

回答by rahul

Use the :notselector

使用:not选择器

回答by nategood

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

您是否尝试过使用带有要排除的 id 的“not”选择器?

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()