DOM note (6): How to Implement JQuery extension ?, Domjquery
1. Global Function Extension
A global function adds an independent function to the namespace of JQuery. You can use $. fucnName (param) or jQuery. funcName (param) to call the API.
1. Add a global function sayHello on JQuery directly.
JQuery. sayHello = function (name) {alert (name + "hello ");};
Call method:
JQuery. sayHello ("Zhang zihan"); // or use $. sayHello ("Zhang zihan ");
2. jQuery. extend (): it extends an object by using one or more objects and returns the extended objects.
2.1 jQuery. extend (dsc, src1, src2 ...) : Extend src1 and src2 to the dsc object and return the extended dsc object (merged object)
2.2 jQuery. extend (object): Extension of the jQuery namespace itself. The result is to add a function to the jQuery namespace.
// 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 Method
jQuery.min(2,3); // => 2jQuery.max(4,5); // => 5
Ii. Extension of object Methods
The extension of object methods allows all methods called by jQuery objects.
1. jQuery. fn
// Add the sayHello method jQuery. fn. sayHello = function (name) {alert (name + "") on the jQuery object ");}
Call
$("div").sayHello("dwqs");
2. jQuery. fn. extend
Extends the jQuery element set to provide new methods (usually used to create plug-ins ).
// 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();
In the next note, we will use a simple example to explain how to develop plug-ins in this way.
First: http://www.ido321.com/1330.html
Next article: mathematical principles of search and web page ranking