开发者

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

开发者 https://www.devze.com 2022-12-16 06:04 出处:网络
<div id=\"target\"> <div id=\"exclude\"></div> <div></div> ... </开发者_高级运维div>
<div id="target">
    <div id="exclude"></div>
    <div></div>
    ...
</开发者_高级运维div>

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


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:

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

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


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

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


Use the :not selector


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


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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消