jQuery.extend函数

萝らか妹 提交于 2019-12-01 05:36:47

1 扩展jQuery静态方法

$.extend({
    test:function(){alert('test函数');}
})

用法: $.test()

2 合并多个对象

以jQuery.extend(css1,css2)为例,css1和css中有一些属性,extend函数会把css2中有的而在css1中没有的属性加到css1中,如果css2中的某个属性在css1中也有,则会用css2的属性覆盖css1的同名属性,css1就是最后的整合对象。或者也可以用用:

var newcss = jquery.extend(css1,css2) newcss就是合并的新对象

var newcss = jquery.extend({},css1,css2) newcss就是合并的新对象,而且没有破坏css1的结构。

//用法: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"}

3 深度嵌套对象

新的extend()允许更深度的合并镶套对象。

//以前的 .extend()

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

4 API网址http://api.jquery.com/jQuery.extend/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!