如何从 jQuery 选择中排除这些元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1002319/
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
How can I exclude these elements from a jQuery selection?
提问by Malfist
My jQuery:
我的jQuery:
$("ul.dropdown ul").slideDown("slow");
$("ul.dropdown ul ul").children().hide();
That causes the ones that match the second selector to display for a brief time. How can I exclude the second set from the first set, and only show the first set?
这会导致与第二个选择器匹配的那些短时间显示。如何从第一组中排除第二组,而只显示第一组?
My HTML:
我的 HTML:
<ul class="dropdown">
<li style="margin: 0px">
<span id="header">
<img src="back.gif" alt='background' style="border:none;" />
<span style="position: absolute; top: 5px; left: 2px;">
<img src="button.gif" style="border:none;" />
</span>
<span style=" position: absolute; top: -5px; left: 70px;">
<p style="background-color: white; width: 200px; height: 20px; font-size: 1.2em; border: 2px solid blue">Menu</p>
</span>
</span>
<ul class="sub_menu">
<li><a href="#">Artificial Turf</a></li>
<li><a href="#">blah</a></li>
<li id="1">
<a id="1.1" href="#">Batting Cages</a>
<ul id="2">
<li><a href="#">Indoor</a></li>
<li><a href="#">Outdoor</a></li>
</ul>
</li>
<li><a href="#">Benches & Bleachers</a></li>
<li><a href="#">Communication Devices</a></li>
<li><a href="#">Dugouts</a></li>
<li><a href="#">Fencing & Windscreen</a></li>
<li><a href="#">Floor Protectors</a></li>
<li><a href="#">Foul Poles</a></li>
<li><a href="#">Netting</a></li>
<li><a href="#">Outdoor Furniture & Storage</a></li>
<li><a href="#">Outdoor Signs</a></li>
<li><a href="#">Padding</a></li>
<li><a href="#">Scoreboards</a></li>
<li><a href="#">Shade Structures</a></li>
<li><a href="#"> - VIEW ALL - </a></li>
</ul>
</li>
</ul>
采纳答案by jwl
This will probably work:
这可能会起作用:
$("ul.dropdown > li > ul").slideDown("slow");
">" means only direct children (http://docs.jquery.com/Selectors/child#parentchild). So as long as your heirarchy is like this (which, after your edit just now, it appears it does):
">" 仅表示直接子级 ( http://docs.jquery.com/Selectors/child#parentchild)。因此,只要您的层次结构是这样的(在您刚刚编辑之后,它似乎确实如此):
<ul>
<li>
<ul>
..
it will most likely work. Hopefully relevant variations are obvious...
它很可能会起作用。希望相关的变化是显而易见的......
回答by ozan
This is exactly what the 'not' methodis for:
这正是“not”方法的用途:
$('ul.dropdown ul').not('ul.dropdown ul ul').slideDown('slow');
回答by albertein
This could work.
这可以工作。
$("ul.dropdown ul:not(ul > ul)").slideDown("slow");
$("ul.dropdown ul ul").children().hide();