Now jquery is a popular component. You can use the extension plug-in method to customize the function. You can refer to the following method to create your own plug-in. Jquery provides two methods for development plug-ins:
Jquery. Extend (object); adds a new method to the class to extend the jquery class itself.
Jquery. FN. Extend (object); add a method to the jquery object.
What is fn. View jquery Code It is not difficult to find out.
Copy code The Code is as follows:
Jquery. fn = jquery. Prototype = {
Init: function (selector, context ){//....
//......
};
It turns out that jquery. fn = jquery. prototype. It is certainly no stranger to prototype.
Although JavaScript does not have a clear concept of a class, it is more convenient to use a class to understand it.
Jquery is a well-encapsulated class. For example, we use the statement $ ("# btn1") to generate a jquery class instance.
Jquery. Extend (object); adding class methods to the jquery class can be understood as adding static methods. For example:
Copy code The Code is as follows:
$. Extend ({
Add: function (a, B) {return a + B ;}
});
Add a "static method" for jquery as add, 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 "member functions" to the jquery class ". Jquery class instances can use this "member function ".
For example, we want to develop a plug-in and create a Special edit box. When it is clicked, the content in the current edit box of alert is displayed. You can do this:
Java code
Copy code The Code is as follows:
$. FN. Extend ({
Alertwhileclick: function (){
$ (This). Click (function (){
Alert ($ (this). Val ());
});
}
});
$ ("# Input1"). alertwhileclick (); // <input id = "input1" type = "text"/>
$. FN. Extend ({
Alertwhileclick: function (){
$ (This). Click (function (){
Alert ($ (this). Val ());
});
}
});
$ ("# Input1"). alertwhileclick (); // <input id = "input1" type = "text"/>
$ ("# Input1") is a jquery instance. When it calls the member method alertwhileclick, the extension is implemented. When it is clicked, the content in the current edit is displayed.
In the real development process, of course, we will not do such a small plug-in. In fact, jquery has provided a wide range of operations.CompositionFile, event, CSS, Ajax, and effect methods can be combined to develop more niubility plug-ins.