An extension of global functions
A global function is to add a separate function to the namespace central of jquery. When used, it can be invoked either through $.fucnname (param) or jquery.funcname (param).
1. Add directly to jquery: Add a global function to jquery SayHello
The code is as follows |
|
Jquery.sayhello=function (name) { Alert (name+ "Hello"); }; |
Call Mode:
The code is as follows |
|
Jquery.sayhello ("Zhang Zihan"); or use $.sayhello ("Zhang Zihan"); |
2, Jquery.extend (): Its role is to use one or more objects to extend an object, to return the extended object.
2.1 Jquery.extend (Dsc,src1,src2 ...): extends the SRC1,SRC2 to the DSC object and returns the Extended DSC object (the merged object)
2.2 Jquery.extend (object): Expands the jquery namespace itself, resulting in the addition of functions to the jquery namespace.
The code is as follows |
|
Add two functions to the jquery namespace. Jquery.extend ({ Min:function (A, b) {return a < b a:b;}, Max:function (A, b) {return a > b a:b;} }); |
Call mode
The code is as follows |
|
Jquery.min (2,3); => 2 Jquery.max (4,5); => 5 |
Ii. extension of Object methods
The extension of the object method allows all the methods called by the jquery object.
1, Jquery.fn
The code is as follows |
|
Add the SayHello method to the JQuery object Jquery.fn.sayhello=function (name) { Alert (name+ "Hello"); }
Call $ ("div"). SayHello ("Dwqs"); |
2, JQuery.fn.extend
Expand the set of JQuery elements to provide new methods (usually used to make plug-ins).
code is as follows |
&nbs P; |
//Add two plug-in methods. JQuery.fn.extend ({ check:function () { return This.each (function () { This.checked = true; }); }, uncheck:function () { return This.each (function () {this.checked = false; }); } }); Call: $ ("Input[type=checkbox]"). Check (); $ ("Input[type=radio]"). Uncheck (); |