1.1 add static methods
- Jquery. Extend (object );
CopyCode
To expand the jquery class itself and add new methods to the class, you can understand how to add static methods.
- $. Extend ({
- Addmethod: function (a, B) {return a + B;} // $. addmethod (1, 2); // return 3
- });
Copy code
1.2 add member Method
- Jquery. FN. Extend (object );
Copy code
- Jquery. fn = jquery. Prototype
Copy code
Add a method to the jquery object, expand jquery. prototype, and add a member method to the jquery class:
- $. FN. Extend ({
- Getinputtext: function (){
- $ (This). Click (function (){
- Alert ($ (this). Val ());
- });
- }
- });
- $ ("# Username"). getinputtext ();
Copy code
2. A general framework:
The following is a general framework:
- (Function ($ ){
- $. FN. yourpluginname = function (options ){
- // Various attributes and Parameters
- VaR Options = $. Extend (defaults, options );
- This. Each (function (){
- // Implementation code of the plug-in
- });
- };
- }) (Jquery );
Copy code
About
- $. Extend (defaults, options );
Copy code
Ults is extended by merging defaults and options to implement the default Parameter Function of the plug-in. For details, refer to the official document of jquery:
Http://api.jquery.com/jQuery.extend/
Original article: http://www.itzhai.com/jquery-plug-ins-to-achieve-the-methods-and-principles-of-simple-instructions.html