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

  • 相关阅读:
    Linux之文档与目录结构
    Linux介绍
    CentOS7下zip解压和unzip压缩文件
    yum 命令讲解
    Linux安装redis
    pip更新问题
    第一章-KS8初体验 安装部署
    MVC Razor视图引擎
    MVC 组件之间的关系
    Web应用程序和网站的区别
  • 原文地址:https://www.cnblogs.com/bigdesign/p/4432283.html
Copyright © 2011-2022 走看看