国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery $.extend()用法總結

2019-11-20 14:29:16
字體:
來源:轉載
供稿:網友
jQuery為開發插件提拱了兩個方法,分別是:
jQuery.fn.extend(object);

jQuery.extend(object);
jQuery.extend(object);為擴展jQuery類本身.為類添加新的方法。
jQuery.fn.extend(object);給jQuery對象添加方法。這個應該很好理解吧。舉個例子。
復制代碼 代碼如下:

<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>

好的,上面你也看到了一點點$.extend()的用法。

1.合并多個對象。

這里使用的就是$.extend()的嵌套多個對象的功能。

所謂嵌套多個對象,有點類似于數組的合并的操作。

但是這里是對象。舉例說明。
復制代碼 代碼如下:

<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)
//結果:Css1的size屬性被覆蓋,而且繼承了Css2的weight屬性
// Css1 = {size: "12px",style: "oblique",weight: "bolder"}
</span>

2.深度嵌套對象。
復制代碼 代碼如下:

<span style="font-size:18px;"> jQuery.extend(
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
// 結果:
// => { name: “John”, last: “Resig”, location: { state: “MA” } }
// 新的更深入的 .extend()
jQuery.extend( true,
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
// 結果
// => { name: “John”, last: “Resig”,
// location: { city: “Boston”, state: “MA” } }
</span>

3.可以給jQuery添加靜態方法。
復制代碼 代碼如下:

<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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀仁县| 彭阳县| 确山县| 红河县| 米林县| 中山市| 诸暨市| 衡南县| 淮南市| 措勤县| 尼勒克县| 阿荣旗| 上虞市| 新营市| 昌都县| 贺州市| 贵溪市| 东源县| 苍溪县| 明水县| 崇文区| 遵义市| 石城县| 大新县| 汶川县| 永吉县| 玉溪市| 贞丰县| 宜阳县| 莱西市| 六安市| 富裕县| 青冈县| 永靖县| 贺兰县| 濮阳市| 滨州市| 金川县| 民勤县| 吴江市| 长垣县|