jquery为开发插件提拱了两个方法,分别是:
jquery.fn.extend(object);
jquery.extend(object);
jquery.extend(object);为扩展jquery类本身.为类添加新的方法。
jquery.fn.extend(object);给jquery对象添加方法。这个应该很好理解吧。举个例子。
new soul
new soul
new soul
new soul
<script type="text/javascript" src="jquery.2.0.3.js"></script>
<script type="text/javascript"> <br>jQuery.fn.myPlugin = function(options) { <br>$options = $.extend( { <br>html: "no messages", <br>css: { <br>"color": "red", <br>"font-size":"14px" <br>}}, <br>options); <br>return $(this).css({ <br>"color": $options.css.color, <br><br>}).html($options.html); <br>} <br><br><br>$('.ye').myPlugin({html:"So easy,yes?",css:{"color":"green","font-size":"20px"}}); <br></script>
好的,上面你也看到了一点点$.extend()的用法。
1.合并多个对象。
这里使用的就是$.extend()的嵌套多个对象的功能。
所谓嵌套多个对象,有点类似于数组的合并的操作。
但是这里是对象。举例说明。
//用法: 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"}
2.深度嵌套对象。
随缘网络PHP企业网站管理系统V2.0正式发布,该企业网站管理系统采用PHP+MYSQL编写,界面色调风格延续之前1.0版管理系统简洁浅蓝色风格,稍有所变动。变更分类树形目录方式采用jquery库,产品,文章三级无限分类。希望大家能够喜欢。系统中难免有些小问题,希望大家在使用中有什么问题可到本站论坛提出,我们将总结各问题后给予修正并升级。本站再次声明对于免费版系列系统本站不提供QQ电话等技术咨询服
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” } }
3.可以给jQuery添加静态方法。
<script type="text/javascript" src="jquery.2.0.3.js"></script>
<script type="text/javascript"> <br>$.extend({ <br>add:function(a,b){return a+b;}, <br>minus:function(a,b){return a-b}, <br>multiply:function(a,b){return a*b;}, <br>divide:function(a,b){return Math.floor(a/b);} <br>}); <br><br>var sum = $.add(3,5)+$.minus(3,5)+$.multiply(3,5)+$.divide(5,7); <br>console.log(sum); <br></script>









