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

  • 相关阅读:
    ixgb 中断
    libvirt
    docker 查看虚拟机xml
    什么是可串行化MVCC
    算法题:实现 strStr()函数
    Python库 numpy基础内容学习笔记
    python3.6+torch1.2实现Sentiment Analysis(数据集MR)
    人工智能能力提升指导总结
    深度学习入门篇01(Tensorflow-gpu的安装)
    走进PEP8——代码规范
  • 原文地址:https://www.cnblogs.com/bigdesign/p/4432283.html
Copyright © 2011-2022 走看看