.size()
.size() 返回: Number
描述: 返回的jQuery對象匹配的DOM元素的數(shù)量。
version added: 1.0.size()
假設(shè)頁面上有下面一個簡單的無序列表:
<ul> <li>foo</li> <li>bar</li> </ul>
我們通過調(diào)用 .size() 來獲取列表項的數(shù)目:
alert('Size: ' + $('li').size());
于是就顯示了列表項的個數(shù):
Size: 2
你也可以使用 .length 屬性來代替,他會略微快那么一點點。
Example:
統(tǒng)計divs。 點擊添加更多。
<!DOCTYPE html>
<html>
<head>
<style>body { cursor:pointer; }
div { width:50px; height:30px; margin:5px; float:left; background:blue; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<span></span>
<div></div>
<script>$(document.body).click(function () { $(document.body).append($("<div>"));
var n = $("div").size();
$("span").text("There are " + n + " divs." + "Click to add more.");}).click(); // trigger the click to start</script>
</body>
</html>