jquery Plugin Development method

Source: Internet
Author: User

I. jquery expansion /c0>

  1,$.extend (object)

Similar to. NET extension method for extending jquery. It can then be called in the form of $.

    $ (function() {        function () {alert ("extend a fun1 function for jquery!") ); } });        $.fun1 ();    })    

  2, $.fn.extend (object)

The object that extends jquery.

    function () {alert ("execution Method 2");}});    $ ("#id1"). Fun2 (); 

You can use Google to see:

  

The above notation is equivalent to:

    function () {alert ("execution Method 2");}    $ (this). fun2 ();  
Second, private domain

It is defined in the following way:

(function ($) {}) (jQuery); // equivalent to function (xxoo) {};FN (jQuery);     

The following code pops up 123.

  $ (function() {     function (xxoo) {};     fn (Alert (123));})    

Iii. Basic steps for defining plug-ins

  1. Define Scope

The development of a jquery plug-in, the first to isolate the plug-in code with the outside world, the external code does not allow direct access to the plug-in internal code, plug-in internal code does not affect the external.

    Step 1 defines the plug-in private scope    (function ($) {    }) (jQuery); 

  This will ensure that the code inside the plugin is isolated from the outside world.

  2. Extending jquery

After defining the scope, you need to extend the plug-in to jquery in order for the external calls to be made.

    Step 1 Define the private scope    (function ($) {        ///Step 2 plug-in extension method name        function (options) {}}) ( jQuery);     

  3. Default value

After you have defined the jquery plug-in, you can specify it in this way if you want some parameters to have default values.

    Step 1 Define the private scope    (function ($) {        //Step 3 Plugin Default Value property        var defaults = {Id: ' #id1 '/// functionvar options = $.extend (defaults, options);}}) (jQuery);         

  4. Support jquery Selector

    //Step 1 Define the private scope (function ($) { /// Step 3 Plugin Default value attribute var defaults = {Id: ' #id1 ',}; // Step 2 plug-in extension method name $.fn. MyFrame = function (options) { // Step 3 Merge user-defined properties, default properties (use Defaults if options are empty) var options =  $.extend (defaults, options); } //step 4 supports jquery selector This.each (function () {});}) (jQuery);                  

  5, support the chain Call of jquery

    //Step 1 Define the private scope (function($) {//Step 3 Default Value properties for plug-insvar defaults = {Id: ' #id1 ',}; // Step 2 plug-in extension method name $.fn. MyFrame = function (options) { // Step 3 Merge user-defined properties, default properties (if options are empty, use defaults) var options = $. Extend (defaults, options); } // Step 4 support jquery selector // Step 5 Support chaining call (return step 4) return  This.each (function () {});}) (jQuery);                  

  6, plug-in internal methods

  

    //Step 1 Define the private scope (function($) {//Step 3 Default Value properties for plug-insvar defaults ={Id: ' #id1 ', };Step 6 Define the function in the pluginvar myfun =function(obj) {alert (obj);}// Step 2 plug-in extension method name $.fn. MyFrame = function (options) { // Step 3 Merge user-defined properties, default properties (if options are empty, use defaults) var options = $. Extend (defaults, options); } // Step 4 support jquery selector // Step 5 Support chaining call (return step 4) return  This.each (function () { // Step 6 Define the function myfun (this) in the plugin;}); }) (jQuery);                  

Because of the scope relationships, the private functions of step 6 are currently allowed for internal use by plug-ins.

jquery Plugin Development method

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.