jQuery length和size()区别总结如下:
1.length是属性,size()是方法。

2.如果你只是想获取元素的个数,两者效果一样既 $("img").length 和 $("img").size() 获取的值是一样的。

3.如果计算一个字符串的长度或者计算一个数组元素的个数就只得用length, 如 $("#text").val().length。

 看看他们的执行时间,http://jsperf.com/size-vs-length用这个检测的

jQuery length 和 size()区别

从图中可以看到size()方法比length慢38%,原因何在?

原因在此:

jQuery length 和 size()区别

看看官网的解释(http://api.jquery.com/size/):

The .size() method is deprecated as of jQuery 1.8. Use the .length property instead.

The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call.
从上可以看出size()是调用length属性实现的

在jquery 1.8后 length取代了 size()  ,因为length不需要返回一个函数调用,更优秀。

相关文章:

  • 2021-08-29
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2021-07-25
相关资源
相似解决方案