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;
    }

  • 相关阅读:
    [Lydsy1706月赛]大根堆
    某考试 T1 Function
    [SHOI2016] 随机序列
    某考试 T1 至危警告
    某考试 T2 yja
    bzoj1880 [Sdoi2009]Elaxia的路线
    bzoj1804 [Ioi2007]Flood 洪水
    bzoj4546 codechef XRQRS
    bzoj4547 小奇的集合
    bzoj1443 [JSOI2009]游戏Game
  • 原文地址:https://www.cnblogs.com/bigdesign/p/4432283.html
Copyright © 2011-2022 走看看