zoukankan      html  css  js  c++  java
  • Jquery中$与$.fn的区别

    1.$本质上其实是Jquery的另一种表现形式

    2.Jquery方法拓展(类似于java类中添加静态方法)

            (function ($) {
                $.test2 = function (a, b)
                {
                    alert(a+b);
                }
            })(jQuery);
            $.test2(1,5);
    方法1
            (function ($) {
                $.extend({
                    test: function (a, b) {
                        alert(a-b)
                    }
                });
            })(jQuery);
            $.test(2,1);
    方法2

    3.$.fn是指jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效

            (function ($) {
                $.fn.add = function (a, b) {
                    alert(a + b);
                }
            })(jQuery);
            $("#source").add(1,4);
    View Code
            (function ($) {
                $.fn.extend({
                    add:function(a,b)
                    {
                        alert(a + b);
                    }
                });
            })(jQuery);
            $("#source").add(1, 4);
    方法2

    jQuery.fn = jQuery.prototype

    源码:jQuery.fn = jQuery.prototype = { 
       init: function( selector, context ) {//....  
       //...... 
    }; 

  • 相关阅读:
    Python Day7(相关补充)
    Python Day7
    Python Day6
    Python Day5
    Python Day4
    Python Day3
    Python Day2
    Python Day1
    复杂装饰器原理分析
    Unity 坐标
  • 原文地址:https://www.cnblogs.com/liandy0906/p/7351772.html
Copyright © 2011-2022 走看看