Home > Article > Web Front-end > jQuery $.extend() usage summary
This article mainly introduces the usage of jQuery $.extend(). Friends who need it can refer to it
jQuery provides two methods for developing plug-ins, which are:
jQuery.fn.extend(object);
jQuery.extend(object);
jQuery.extend(object); To extend the jQuery class itself. Add new methods to the class.
jQuery.fn.extend(object); Add methods to jQuery objects. This should be easy to understand. for example.
Copy code The code is as follows:
font-size:18px;">
new soul
new soul
< h3 class="ye">new soul
Okay, you also saw a little $.extend above () usage.
1. Merge multiple objects.
What is used here is the function of nesting multiple objects of $.extend().
The so-called nesting of multiple objects is somewhat similar to the operation of merging arrays.
But here is the object. for example.
Copy code The code is as follows:
//Usage: jQuery. extend(obj1,obj2,obj3,..)
var Css1={size: "10px",style: "oblique"}
var Css2={size: "12px",style: "oblique",weight : "bolder"}
$.jQuery.extend(Css1,Css2)
//Result: Css1's size attribute is overwritten, and inherits 's weight from Css2 Attribute
// Css1 = {size: "12px",style: "oblique",weight: "bolder"}
2. Depth Nested objects.
Copy code The code is as follows:
jQuery.extend(
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
// result :
// => { name: “John”, last: “Resig”, location: { state: “MA” } }
// New deeper.extend()
jQuery .extend( true,
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
) ;
// Result
// => { name: “John”, last: “Resig”,
// location: { city: “Boston”, state: “MA” } }
3. You can add static methods to jQuery.
Copy code The code is as follows:
< /span>
The above is the detailed content of jQuery $.extend() usage summary. For more information, please follow other related articles on the PHP Chinese website!