jQuery $.extend()用法總結(jié)
jQuery.fn.extend(object);
jQuery.extend(object);
jQuery.extend(object);為擴(kuò)展jQuery類(lèi)本身.為類(lèi)添加新的方法。
jQuery.fn.extend(object);給jQuery對(duì)象添加方法。這個(gè)應(yīng)該很好理解吧。舉個(gè)例子。
<span style="font-size:18px;"><html>
<head>
<title></title>
</head>
<body>
<h3 class="ye">new soul</h3>
<h3 class="ye">new soul</h3>
<h3 class="ye">new soul</h3>
<h3 class="ye">new soul</h3>
<script type="text/javascript" src="jquery.2.0.3.js"></script>
<script type="text/javascript">
jQuery.fn.myPlugin = function(options) {
$options = $.extend( {
html: "no messages",
css: {
"color": "red",
"font-size":"14px"
}},
options);
return $(this).css({
"color": $options.css.color,
}).html($options.html);
}
$('.ye').myPlugin({html:"So easy,yes?",css:{"color":"green","font-size":"20px"}});
</script>
</body>
</html>
</span>
好的,上面你也看到了一點(diǎn)點(diǎn)$.extend()的用法。
1.合并多個(gè)對(duì)象。
這里使用的就是$.extend()的嵌套多個(gè)對(duì)象的功能。
所謂嵌套多個(gè)對(duì)象,有點(diǎn)類(lèi)似于數(shù)組的合并的操作。
但是這里是對(duì)象。舉例說(shuō)明。
<span style="font-size:18px;">//用法: jQuery.extend(obj1,obj2,obj3,..)
var Css1={size: "10px",style: "oblique"}
var Css2={size: "12px",style: "oblique",weight: "bolder"}
$.jQuery.extend(Css1,Css2)
//結(jié)果:Css1的size屬性被覆蓋,而且繼承了Css2的weight屬性
// Css1 = {size: "12px",style: "oblique",weight: "bolder"}
</span>
2.深度嵌套對(duì)象。
<span style="font-size:18px;"> jQuery.extend(
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
// 結(jié)果:
// => { name: “John”, last: “Resig”, location: { state: “MA” } }
// 新的更深入的 .extend()
jQuery.extend( true,
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
// 結(jié)果
// => { name: “John”, last: “Resig”,
// location: { city: “Boston”, state: “MA” } }
</span>
3.可以給jQuery添加靜態(tài)方法。
<span style="font-size:18px;"><html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="jquery.2.0.3.js"></script>
<script type="text/javascript">
$.extend({
add:function(a,b){return a+b;},
minus:function(a,b){return a-b},
multiply:function(a,b){return a*b;},
divide:function(a,b){return Math.floor(a/b);}
});
var sum = $.add(3,5)+$.minus(3,5)+$.multiply(3,5)+$.divide(5,7);
console.log(sum);
</script>
</body>
</html></span>
相關(guān)文章
jquery中attr、prop、data區(qū)別與用法分析
這篇文章主要介紹了jquery中attr、prop、data區(qū)別與用法,結(jié)合實(shí)例形式分析了jQuery中attr、prop、data的區(qū)別、功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-09-09jQuery動(dòng)畫(huà)animate方法使用介紹
用于創(chuàng)建自定義動(dòng)畫(huà)的函數(shù)。這個(gè)函數(shù)的關(guān)鍵在于指定動(dòng)畫(huà)形式及結(jié)果樣式屬性對(duì)象,下面為大家詳細(xì)介紹下animate方法的具體使用,感興趣的朋友可以了解下哈2013-05-05jQuery+ajax實(shí)現(xiàn)鼠標(biāo)單擊修改內(nèi)容的方法
這篇文章主要介紹了jQuery+ajax實(shí)現(xiàn)鼠標(biāo)單擊修改內(nèi)容的方法,需要的朋友可以參考下2014-06-06jquery實(shí)現(xiàn)模擬百分比進(jìn)度條漸變效果代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)模擬百分比進(jìn)度條漸變效果代碼,涉及jQuery基于時(shí)間函數(shù)操作頁(yè)面元素樣式變換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10jQuery 選擇同時(shí)包含兩個(gè)class的元素的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇jQuery 選擇同時(shí)包含兩個(gè)class的元素的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06Ajax異步獲取html數(shù)據(jù)中包含js方法無(wú)效的解決方法
本文主要介紹了Ajax異步獲取html數(shù)據(jù)中包含js方法無(wú)效的解決方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02firefox下jquery iframe刷新頁(yè)面提示會(huì)導(dǎo)致重復(fù)之前動(dòng)作
刷新頁(yè)面會(huì)提示要顯示此頁(yè)面, Firefox 必須發(fā)送將會(huì)導(dǎo)致重復(fù)之前動(dòng)作的數(shù)據(jù),此問(wèn)題很是疑惑,接下來(lái)將為您解答,需要的朋友可以了解下2012-12-12jQuery序列化表單成對(duì)象的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇jQuery序列化表單成對(duì)象的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11