jquery插件

jQuery爲開發插件提拱了兩個方法,分別是:css

jQuery.fn.extend(object); jquery

jQuery.extend(object);this

jQuery.extend(object); 爲擴展jQuery類自己.爲類添加新的方法。spa

jQuery.fn.extend(object);給jQuery對象添加方法prototype

 

 

fn 是什麼東西呢。查看jQuery代碼,就不難發現。插件


jQuery.fn = jQuery.prototype = { code

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

   //...... blog

}; ip

原來 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>
相關文章
相關標籤/搜索