.size()


.size()返回值: 整数版本弃用: 1.8, 已移除: 3.0

描述: 返回 jQuery 对象中的元素数量。

  • 版本添加: 1.0.size()

    • 此方法不接受任何参数。

注意: 此方法已在 jQuery 3.0 中移除。请使用 .length 属性代替。

.size() 方法在功能上等同于 .length 属性;但是,.length 属性更可取,因为它没有函数调用的开销。

给定页面上一个简单的无序列表

1
2
3
4
<ul>
<li>foo</li>
<li>bar</li>
</ul>

.size().length 都识别项目数量

1
2
alert( "Size: " + $( "li" ).size() );
alert( "Size: " + $( "li" ).length );

这将导致两个警报

大小: 2

大小: 2

示例

统计 div 的数量。

1
2
3
4
5
6
7
8
9
$( document.body )
.on( "click", function() {
$( this ).append( $( "<div>" ) );
var n = $( "div" ).size();
$( "span" ).text( "There are " + n + " divs. Click to add more." );
} )
// Trigger the click to start
.trigger( "click" );