jquery插件

jQuery为开发插件提拱了两个方法,分别是:

jQuery.fn.extend(object);

jQuery.extend(object);

jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。

jQuery.fn.extend(object);给jQuery对象添加方法

fn 是什么东西呢。查看jQuery代码,就不难发现。


jQuery.fn = jQuery.prototype = {

   init: function( selector, context ) {//.... 

   //......

};

原来 jQuery.fn = jQuery.prototype.

jQuery便是一个封装得非常好的类,比如我们用 语句 $("#btn1") 会生成一个 jQuery类的实例。

jQuery.extend(object); 为jQuery类添加添加类方法,可以理解为添加静态方法。如:

$.extend({

  add:function(a,b){return a+b;}

});

$.add(3,4);  //return 7

jQuery.fn.extend(object); 对jQuery.prototype进行扩展

<script>
//            $.fn.changeColor=function(color){
//                this.css('background',color);  //this指向调用它的对象box
//            };
                    $.fn.extend({
                        cs:function(name,value){
                            this.css(name,value);   //this是jquery对象
                        },
                    });
        </script>
        <script>
                $(function(){
                    $('#box').cs('background','red');
                    $('#box1').cs('background','green');
                    $('#box1').cs('width','300px');
                });
        </script>
原文地址:https://www.cnblogs.com/yang0902/p/5719937.html