如何计算没有类.extra?
的元素.em
var cnt = $('.em').not('.extra').length;
console.log(cnt);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="em">
<div class="header">aaa</div>
<ul>nnn</ul>
</div>
<div class="em">
<div class="header">aaa</div>
<div class="extra">eee</div>
<ul>nnn</ul>
</div>
<div class="em">
<div class="header">aaa</div>
<ul>nnn</ul>
</div>
&#13;
答案 0 :(得分:4)
您使用.em
类来否定.extra
个元素。由于.em
元素都没有类.extra
,因此所有元素都被选中。
一种选择是将:not()
与:has()
selector合并:.em:not(:has(.extra))
var cnt = $('.em:not(:has(.extra))').length;
console.log(cnt);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="em">
<div class="header">aaa</div>
<ul>nnn</ul>
</div>
<div class="em">
<div class="header">aaa</div>
<div class="extra">eee</div>
<ul>nnn</ul>
</div>
<div class="em">
<div class="header">aaa</div>
<ul>nnn</ul>
</div>
&#13;