Javascript jQuery:选择没有特定类的所有子元素

Javascript jQuery:选择没有特定类的所有子元素,javascript,jquery,html,Javascript,Jquery,Html,我有以下HTML结构: <li class="menu-has-children"><a href="#">Main</a> <ul class="some-menu"> <li><a href="#">Some Link 1</a></li> <li class="not-wanted"><a href="#">Some Link 2</a><

我有以下HTML结构:

<li class="menu-has-children"><a href="#">Main</a>
<ul class="some-menu">
    <li><a href="#">Some Link 1</a></li>
    <li class="not-wanted"><a href="#">Some Link 2</a></li>
    <li><a href="#">Some Link 3</a></li>
</ul>
</li>
我的目标是在
之后显示下一个
ul
元素的所有
li
元素,但不包括附加了
不需要
类的li元素。我怎样才能做到这一点


到目前为止,我只按照上述代码的含义显示所有
ul
元素,但我不知道如何排除不需要的元素。

您可以使用jQuery
not
函数:

$('li').not('.not-wanted')

对于您的情况,它可能看起来像这样:

$(document).on('click', '.menu-has-children a', function(e) {
  $(this).nextAll('ul').find('li').not('.not-wanted').slideToggle();
});

这与使用CSS3
not
限定符获得相同的结果,但可能更容易阅读。我无法保证它在速度上与CSS3
not
-这需要测试。

您可以使用CSS3
not
选择器:

$(document).on('click', '.menu-has-children a', function(e) {
  $(this).nextAll('ul').find("li:not(.not-wanted)").slideToggle();
});

这样做,

$(document).on('click', '.menu-has-children a', function(e) {
  $('li:not(.not-wanted)', $(this).next('ul')).slideToggle();
});
:not()
选择器将帮助您从整个选择中排除一组元素


最好的答案是imhoThanks Aleks,但完整的解决方案是什么?我应该把你的代码放在哪里?将其附加到
$(this.nextAll('ul').eq(0).slideToggle()
stuff?中,但不幸的是,它不起作用。不再显示任何内容。不幸的是,不起作用。什么也看不出来anymore@chevallier语法有点混乱。因为我很久以前就接触过UI元素。你现在可以查一下,。
$(document).on('click', '.menu-has-children a', function(e) {
  $('li:not(.not-wanted)', $(this).next('ul')).slideToggle();
});