JQ plug-in

Source: Internet
Author: User

I have never known how to write plug-ins. Now, I need to know more about it because of my work needs. So I learned some about plug-ins.

In general, plug-ins are a program Writing Method for better extension.

There are two types of query plug-in development:

1, Class-level plug-in development

1.1Add a new global function

To add a global function, we only need to define it as follows:

jQuery.foo = function() {     alert(‘This is a test. This is only a test.‘);};  

 1.2Add multiple global functions

Jquery. foo = function () {alert ('this is a test. this is only a test. ');}; jquery. bar = function (PARAM) {alert ('this function takes a parameter, which is "'+ Param + '". ') ;}; // The call time is the same as that of a function: jquery. foo (); jquery. bar (); or $. foo (); $. bar ('bar'); // You can also. VaR AA = {}; AA. foo = function () {alert ('this is a test. this is only a test. ') ;}; AA. bar = function (PARAM) {alert ('this function takes a parameter, which is "'+ Param + '". ') ;}; AA. foo ();

 1.3UseJquery. Extend (object );

jQuery.extend({        foo: function() {            alert(‘This is a test. This is only a test.‘);        },        bar: function(param) {            alert(‘This function takes a parameter, which is "‘ + param +‘".‘);        }   });            

 1.4Use namespace

Jquery. myplugin = {FOO: function () {alert ('this is a test. this is only a test. ');}, bar: function (PARAM) {alert ('this function takes a parameter, which is "' + Param + '". ') ;}}; // The namespace function is still a global function. The method used for calling is: $. myplugin. foo (); $. myplugin. bar ('baz ');

 2Object-level plug-in development

Object-level plug-in development requires the following two forms:

Form 1:

(function($){   $.fn.extend({       pluginName:function(opt,callback){             // Our plugin implementation code goes here.         }   })   })(jQuery);   

Form 2:

(function($) {     $.fn.pluginName = function() {        // Our plugin implementation code goes here.   };   })(jQuery);    

Understand this for the moment, and make up later.

JQ plug-in

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.