length
在jQuery对象中元素的数量。
length
目前匹配的元素数量。
.size()
方法将返回相同的数字。(jQuery 3.0版已经删除)
例子
计算 div 数量,点击后会增加一个 div。
<!DOCTYPE html> <html> <head> <style> body { cursor:pointer; } div { width:50px; height:30px; margin:5px; float:left;background:green; } p { color:red; } </style> <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script> </head> <body> <span></span> <div></div> <script>$(document.body).click(function () { $(document.body).append($("<div>"));var n = $("div").length; $("p").text("There are " + n + " divs." + "Click to add more."); }).trigger('click'); // trigger the click to start</script> </body> </html>