VVL1295

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

扩展 jQuery 方法

  扩展 jQuery 方法相当的简单,可以扩展其类方法(即 $.function()),也可以扩展其成员方法(即 $.('xx').function());

  直接上代码

    扩展类方法:

$.extend({
  add:function(a,b){returna+b;}
}); 
// 调用名为 add 的类方法
$.add(3,4); //return 7 

    扩展成员方法:

      方式1:

$.fn.extend({

    alertWhileClick : function() {

        $(this).click(function() {

            alert($(this).val());
        });
    }
});
// 调用名为 alertWhileClick 的成员方法
$('xx').alertWhileClick();

      方式2:

$.fn.serializeObject = function()    
{    
   var o = {};    
   var a = this.serializeArray();    
   $.each(a, function() {    
       if (o[this.name]) {    
           if (!o[this.name].push) {    
               o[this.name] = [o[this.name]];    
           }    
           o[this.name].push(this.value || '');    
       } else {    
           o[this.name] = this.value || '';    
       }    
   });    
   return o;    
};
// 调用该成员方法:
$.('xx').serializeObject ();

 

posted on 2017-01-04 10:41  bobo2018  阅读(100)  评论(0)    收藏  举报