PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

怎样使用jQuery length 和 size()

php中世界最好的语言
php中世界最好的语言 原创
2018-06-02 10:07:55 1401浏览

这次给大家带来怎样使用jQuery length 和 size(),使用jQuery length 和 size()的注意事项有哪些,下面就是实战案例,一起来看一下。

1.length是属性,size()是方法。

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

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

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

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

原因在此:

看看官网的解释(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不需要返回一个函数调用,更优秀。

Learn from yesterday, live for today, hope for tomorrow.

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

如何使用Vue实现拖拽效果

Node.js+console输出日志文件实例分析

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。