jQuery size()的例子
size()是jQuery内置的一个方法,用于查找由给定的选择器匹配的元素的数量。这个方法在jQuery 3.0中被删除,而引入了length()。
语法:
$(selector).size()
参数:它不接受任何参数。
返回值:它返回与所选元素匹配的元素数量。
jQuery代码显示size()方法的工作:
<html>
<head>
<style>
body {
cursor: pointer;
min-height: 100px;
}
div {
width: 50px;
height: 30px;
margin: 5px;
float: left;
background: green;
}
span {
color: black;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js">
</script>
</head>
<body>
<div></div>
<span></span>
<!-- jQuery code to show the working of this method -->
<script>
(document.body)
.click(function() {
(this).append(("<div>"));
var n =("div").size();
$("span").text("There are " + n + " divs. Click to add more.");
})
// Trigger the click to start
.click();
</script>
</body>
</html>
输出:在点击屏幕前-
在屏幕上点击三次后-