First, class level ($.extend)
The class level can be understood as extending the jquery class, the most obvious example being $.ajax (...), which is equivalent to a static method.
The $.extend () method, or Jquery.extend (object), is used when developing its methods.
$.extend ({add:function(A, b) {return A +b;},minus:function(A, b) { Return A-b;}}); // Page Call var i = $.add (3,2); var j = $.minus (3,2);
Second, the object level
The object level can be understood as an object-based extension, such as $ (' #.. '). ChangeColor (...); The ChangeColor here is an object-based extension.
The extension method is developed using the $.fn.extend () method, which is jQuery.fn.extend (object);
$.fn.extend ({check:function() {returnthis. Checked=true;});},uncheck:function() {return This . each ({checked=false;} );}); // The page calls $ (' input[type=checkbox] '). Check (); $ (' input[type=checkbox] '). Uncheck ();
Third, jquery plug-in development process
1. Define the scope: Define a private scope for the plug-in, external code cannot directly access the code inside the plug-in, plug-in internal code does not pollute the global variables
(function($) {// Internal Plug-in code implementation }) (JQuery)
2. Extend a plugin for jquery: Add an extension method to the instance of jquery that can receive some parameters
(function($) {$.fn.mytable=function(options) {//Concrete implementation code }}) ( JQuery)
3. Set Default value: Sets the default value for the extension method and typically defines the default Property object as defaults. Use $.extend (defaults.options) to merge the default values with the passed-in parameters
(function($) {var defaults={name:' plugin ', versions:' 1.0 '}$. fn.mytable=function(options) {var options=$.extend (defaults,options);}}) (jQuery)
4. Support for jquery connection call: Loop to return each element
(function($) {var defaults={name:' My plugin ', Version:' 1.0 '}$. fn.mytable=function(options) {var options=$.extend (defaults,options| | {}); return this. each (function() {// for each element operation });}) (JQuery)
5. Plug-in method: In order to make the code structure visualization, need to use function. The method in the plugin cannot be called by the outside world.
(function($){vardefaults={name:Plug-inversion:' 1.0 '}varShowName =function(obj) {$ (obj). Append (function(){return' Name ';});} $.fn.mytable=function(options) {varoptions=$.extend (defaults,options);return This. each (function() {ShowName ( This);});}}) (jQuery)
6. Custom callback function: Use call and apply method to execute callback function
(function($) {$.fn.outandinfade=function(customoptions) {varoptions = $.extend{},$.fn.outandinfade.defaultoptions,customoptions| |{});return This. each (function(){$( This). On (' click ',function(){if($.isfunction (Options.complete)) {options.complete.apply ( This);}});});}; $.fn.outandinfade.defaultoptions={complete:NULL};}) (jquery);' Div '). Outandinfade ({complete:function(){$( This). CSS (' background ', ' #ff9 ');}});
jquery Encapsulation Plugin