jQuery為開發插件提拱了兩個方法,分別是: 
jQuery.fn.extend(object); 
jQuery.extend(object); 
jQuery.extend(object);為擴展jQuery類本身.為類添加新的方法。 
jQuery.fn.extend(object);給jQuery對象添加方法。這個應該很好理解吧。舉個例子。 
復制代碼 代碼如下:
 
<span><html> 
<head> 
<title></title> 
</head> 
<body> 
<h3>new soul</h3> 
<h3>new soul</h3> 
<h3>new soul</h3> 
<h3>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> 
復制代碼 代碼如下:
 
<span>//用法: 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> 
復制代碼 代碼如下:
 
<span> 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> 
復制代碼 代碼如下:
 
<span><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> 
新聞熱點
疑難解答
圖片精選