kri*_*tya 0 jquery jquery-selectors
如何用jQuery计算匹配的类:contains?
如果我有以下代码:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div class="ll">John Resig</div>
<div class="ll">George Martin</div>
<div>Malcom John Sinclair</div>
<div class="ll">J. Ohn</div>
<script>
$("div.ll").hide()
$("div.ll:contains('John')").show();
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我如何计算匹配的元素?
使用.size()或length
$("div.ll:contains('John')").size();
$("div.ll:contains('John')").length;
Run Code Online (Sandbox Code Playgroud)
你应该使用 length
因为文档说
.size()方法在功能上等同于.length属性; 但是,.length属性是首选属性,因为它没有函数调用的开销.