jQuery选择不包含类的元素 [英] jQuery select elements that do not contain a class
本文介绍了jQuery选择不包含类的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何在这里使用not selector?我想选择所有不包含.image类的.hi类。
How do I use not selector here? I would like to select all the classes ".hi" that do not contain class ".image".
HTML
<div class="hi">
<div class="hue">
<div class="image">
456
</div>
</div>
</div>
<div class="hi">
<div class="hue">
<div class="image">
123
</div>
</div>
</div>
<div class="hi">
<div class="hue">
here
</div>
</div>
JS
我试过这样的事情但它不起作用。
I tried something like this but it didn't work.
console.log($('.hi').find('.hue > :not(.image')));
推荐答案
To do it just with a selector, you'd use :not
with :has
:
$(".hi:not(:has(.image))")
// or to be more specific:
$(".hi:not(:has(.hue > .image))")
<请注意:has
是特定于jQuery的。
Note that :has
is jQuery-specific.
使用过滤器来执行此操作
,你可以在回调中使用 find
:
To do it with filter
, you could use find
in the callback:
$(".hi").filter(function() { return $(this).find(".image").length == 0; })
// or to be more specific:
$(".hi").filter(function() { return $(this).find(".hue > .image").length == 0; })
这篇关于jQuery选择不包含类的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文