<div class="test">
<div class="example"></div>
</div>
<div class="test">
</div>
仅当具有类test
的元素不包含具有类example
的子元素时,我才能将jQuery应用于该元素
发布于 2012-04-16 03:24:31
$('.test:not(:has(.example))')
-或者-
$('.test').not(':has(.example)')
发布于 2012-04-16 03:09:26
有可能
$('.test').filter(function() { return !$(this).children('.example').length; });
这会过滤掉具有与.example
匹配的子元素的所有元素。如果您想要根据子代(而不仅仅是子代)进行过滤,那么可以用.find
代替.children
。
发布于 2012-04-16 03:19:34
$(':not(.test:has(.example))').css('color', 'red');
https://stackoverflow.com/questions/10168248
复制