jquery開發插件的方法$.extend 與 $.fn.extend

jquery開發插件的方法$.extend 與 $.fn.extend


1.$.extend

$.extend(object);爲擴展jQuery類本身.爲類添加新的方法。

//使用方法 
$.extend({
	add:function(a,b){
		return a+b;
	}
})
//JQuery類本身的方法,可以通過$直接調用 
console.log($.add(1,3))  // 4

2. $.fn.extend

$.fn.extend(object)給jQuery對象添加方法。 $.fn 即 $.prototype

//使用方法 
$.fn.extend({
	changeBackground:function(color){
		$(this).css({'background-color':color})
	}
})
//jquery對象的方法,首先要選擇jquery對象 
$("#div").click(function(){
	$(this).changeBackground('red'); //div背景色變紅
})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章