1.异同
1.length是属性,size()是方法。
2.如果你只是想获取元素的个数,两者效果一样既 ("img").length
和("img").length
和("img").size()
获取的值是一样的。
3.如果计算一个字符串的长度或者计算一个数组元素的个数就只得用length, 如 $("#text").val().length
。
4.实质
2.size()
2.1.简述
返回被选中元素的元素个数
2.2.例子
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
alert($("li").size());
});
});
</script>
</head>
<body>
<button>输出 li 元素的数目</button>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
</body>
</html>
3.length
3.1.简述
length 属性包含 jQuery 对象中元素的数目。
3.2.例子
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert($("li").length);
});
});
</script>
</head>
<body>
<button>输出 li 元素的数目</button>
<ul>
<li>咖啡</li>
<li>牛奶</li>
<li>绿茶</li>
</ul>
</body>
</html>