jquery Encapsulation Plugin

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.