zoukankan      html  css  js  c++  java
  • jQuery length和size()区别

    1.length是属性,size()是方法。
    2.如果你只是想获取元素的个数,两者效果一样既 $("img").length 和 $("img").size() 获取的值是一样的;
    3.但是如果是获取字符串的长度就只得用length, 如 $("#text").val().length , 从上可以看出size()是调用length属性实现的,而且在jquery 1.8后 length取代了 size(), 因为length不需要返回一个函数调用,更优秀。

    下面是外国Jquery爱好者关于size()和length区别的回复:
    ①lesgreen【会员】
    To check whether a DOM element is not found, you can check the length:
    var found = $("p").find("span");
    if (found.length == 0) {
    }

    ②Mitur Binesderti【会员】 in reply to lesgreen
    It might be better to use .size() instead.

    ③ David Rodrigues【会员】   in reply to lesgreen
    .size() is about 12% slower that .length in V8 engine
    but it is a invisible difference:
    - .length ~0,0000032s each
    - .size() ~0,0000040s each

    ④Karl Swedberg【会员】 in reply to Michael
    Yes, of course using .size() "will do." But why in the world would you use it instead of .length? It adds no value and it requires an extra function call. From jquery.js:
    size: function() {
    return this.length;
    }

  • 相关阅读:
    团队项目选题参考
    结对编程2——单元测试
    个人作业2——英语学习APP案例分析
    js创建数组
    oracle常用函数
    Oracle聚合函数
    Myeclipse10破解版安装包
    Myeclipse按包装SVN
    Eclipse安装SVN
    Git使用教程
  • 原文地址:https://www.cnblogs.com/bigdesign/p/4432283.html
Copyright © 2011-2022 走看看