閱讀536 返回首頁    go 阿裏雲 go 技術社區[雲棲]


jQuery插件開發中$.extend和$.fn.extend辨析

 jQuery插件開發分為兩種:


1 類級別

類級別你可以理解為拓展jquery類,最明顯的例子是$.ajax(...),相當於靜態方法。

開發擴展其方法時使用$.extend方法,即jQuery.extend(object); 

$.extend({ 

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

       minus:function(a,b){return a-b;} 
}); 

頁麵中調用:

var i = $.add(3,2);

var j = $.minus(3,2);


2 對象級別

對象級別則可以理解為基於對象的拓展,如$("#table").changeColor(...); 這裏這個changeColor呢,就是基於對象的拓展了。

開發擴展其方法時使用$.fn.extend方法,即jQuery.fn.extend(object); 

$.fn.extend({

        check:function(){
              return this.each({
                   this.checked=true;
             });
        },
       uncheck:function(){
              return this.each({
                    this.checked=false;
             });
       }
});

頁麵中調用:

$('input[type=checkbox]').check();
$('input[type=checkbox]').uncheck();


3、擴展

$.xy = {
add:function(a,b){return a+b;} ,
minus:function(a,b){return a-b;},
voidMethod:function(){ alert("void"); }
};
var i = $.xy.add(3,2);
var m = $.xy.minus(3,2);
$.xy.voidMethod();


最後更新:2017-04-02 22:16:20

  上一篇:go Runtime of Objective-C
  下一篇:go 使用資源本地化 ASP.NET 網頁