我需要根据不在其元素内部的内容来选择一个元素。
<div class="person">
<div class="name">Jason</div>
</div>
<div class="person">
</div>
我需要做什么来选择带有 person
类的第二个 div
。我知道有 has
和 not
属性,我是否需要以某种方式组合它们才能使其正常工作?
最佳答案
您可以用作选择器:
$(".person:not(:has(.name))")
但我建议您改为过滤它:
Because :has() is a jQuery extension and not part of the CSS specification, queries using :has() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method.
$(".person").filter(function(){
return !$(this).find(".name").length;
})
-jsFiddle-
关于javascript - 基于内部没有类的 jQuery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28885646/