亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery $.extend()用法總結(jié)

 更新時(shí)間:2014年06月15日 09:49:02   投稿:whsnow  
這篇文章主要介紹了jQuery $.extend()用法,需要的朋友可以參考下
jQuery為開(kāi)發(fā)插件提拱了兩個(gè)方法,分別是:
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è)例子。
復(fù)制代碼 代碼如下:

<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ō)明。
復(fù)制代碼 代碼如下:

<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ì)象。
復(fù)制代碼 代碼如下:

<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)方法。
復(fù)制代碼 代碼如下:

<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)文章

最新評(píng)論