Jquery_如何扩展方法

jQuery 别名 $

一.  类级别扩展方法(比如$.ajax(...))

 1> 单个全局方法

$.testExtend = function (){
     console.log("单个全局方法");
}
# 调用:$.testExtend();

   2> 多方法继承 

$.extend({
	func01 : function(){
		console.log("func01");
	},
	func02 : function(){
		console.log("func02");
	},
	func03 : function(){
		console.log("func03");
	}
}) 
# 调用:$.func01()/$.func02()/$.func03()

 3> 自定义命名空间(个人理解为类,上面两种的结合) 

$.space = {
	func01 : function(){
		console.log("func01");
	},
	func02 : function(){
		console.log("func02");
	},
	func03 : function(){
		console.log("func03");
	}
}
# 调用: $.space.func01();

二.  对象下扩展方法

 1> 最简单的一种

$.fn.func01 = function(){  
	console.log("func01");
}  
# 调用: $("#btn").func01();

    2> 多方法

(function($){     
	$.fn.extend({     
		testing:function(opt,callback){     
			console.log("testing");     
		},
		testing1:function(opt,callback){     
			console.log("testing1");   
		}  		
	})     
})(jQuery);
# 调用:$("#btn").testing($(this),function(){});
(function (ee) {   
	ee.fn.f1 = function (obj) {   
		alert(obj+"f1");  
	};
	ee.fn.f2 = function (obj) {   
		alert(obj+"f2");  
	};
})(jQuery)   
# 调用:$("#btn").f1("hello");

 

posted @ 2018-01-24 13:18  eRrsr  阅读(169)  评论(0编辑  收藏  举报