扩展 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 ();
浙公网安备 33010602011771号