jQuery select 个不包含 class 的元素

jQuery select elements that do not contain a class

我想 select 包含 .hi class 的所有元素 包含 .image class.

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

我试过类似的方法,但没有用:

console.log($('.hi').find('.hue > :not(.image')));

如果只使用选择器,您可以使用 :not with :has:

$(".hi:not(:has(.image))")
// or to be more specific:
$(".hi:not(:has(.hue > .image))")

请注意 :has 是 jQuery 特定的。

要使用 filter,您可以在回调中使用 find

$(".hi").filter(function() { return $(this).find(".image").length == 0; })
// or to be more specific:
$(".hi").filter(function() { return $(this).find(".hue > .image").length == 0; })