jquery has two methods for developing Plug-ins, respectively:
Copy Code code as follows:
JQuery.fn.extend (object);
Jquery.extend (object);
Jquery.extend (object); To extend the jquery class itself. Adds a new method to the class.
JQuery.fn.extend (object); Add methods to the JQuery object.
What is FN? See the jquery code, it's not hard to find.
Copy Code code as follows:
Jquery.fn = Jquery.prototype = {
Init:function (Selector, context) {//....
//......
};
The original Jquery.fn = Jquery.prototype. It's certainly not strange to prototype.
Although JavaScript does not have a well-defined class concept, it is more convenient to use classes to understand it.
jquery is a very nicely encapsulated class, for example, we use statement $ ("#btn1") to generate an instance of a jquery class.
Jquery.extend (object); Adding a class method to the jquery class can be understood as adding a static method. Such as:
Copy Code code as follows:
$.extend ({
Add:function (a,b) {return a+b;}
});
Add a "static method" for jquery, and then you can use this method in the place where jquery is introduced,
$.add (3,4); Return 7
JQuery.fn.extend (object); The extension to Jquery.prototype is to add a "member function" to the jquery class. Examples of jquery classes can use this "member function".
For example, we want to develop a plugin, make a special edit box, when it is clicked, alert the contents of the current edit box. Can do this:
Copy Code code as follows:
$.fn.extend ({
Alertwhileclick:function () {
$ (this). Click ( function () {
Alert ($ (this). Val ());
});
}
});
$ ("#input1"). Alertwhileclick ();//on page: <input id= "INPUT1" type= "text"/>