zoukankan      html  css  js  c++  java
  • jQuery.extend和jQuery.fn.extend的区别

    1、简单来说,jQuery.extend是给jQuery或$这个用的;jQuery.fn.extend是给jQuery的对象(如:$('div'))用的。

    2、创建一个html页面,里面代码为:

    <html>
    <head>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    </head>
    <body>
    <div>jQuery.extend和jQuery.fn.extend的区别</div>
    </body>
    </html>

    3、用jQuery.extend写的扩展

    $.extend({
      test:function(){
        console.log('$.extend function');
      },
      attr:'$.extend attr'
    });

    $.test();//$.extend function

    $.attr;//$.extend attr

    4、用jQuery.fn.extend写的扩展

    $.fn.extend({
      fnTest:function(){
        console.log(this.html());
      },
      fnAttr:'$.fn.extend fnAttr'
    });

    如果直接使用$.fnTest();会报错,$.fnAttr;会报undefined

    使用jQuery对象调用其方法和属性:

    $('div').fnTest();//jQuery.extend和jQuery.fn.extend的区别

    $('div').fnAttr;//$.fn.extend fnAttr

  • 相关阅读:
    盛最多水的容器
    字符串的排序
    整数拆分
    TCP和UDP编程
    旋转图像
    非递减数列
    不同路径2
    不同路径
    压缩拉伸图片
    Java对List分割及使用Spring多线程调用
  • 原文地址:https://www.cnblogs.com/hujiapeng/p/4895667.html
Copyright © 2011-2022 走看看