Two Methods for jQuery plug-in development and $. fn. extend _ jquery

Source: Internet
Author: User
There are two types of jQuery plug-in development: Class 1 and Class 2. The following describes jQuery plug-in development in detail:

Class 1

You can understand the class level as an extension of the jquery class. The most obvious example is $. ajax (...), which is equivalent to a static method.

Use the $. extend method when developing the extension method, that is, jQuery. extend (object );

The Code is as follows:


$. Extend ({

Add: function (a, B) {return a + B ;},

Minus: function (a, B) {return a-B ;}
});


Page call:

The Code is as follows:


Var I = $. add (3, 2 );
Var j = $. minus (3, 2 );


2 object level

The object level can be understood as object-based extension, for example, $ ("# table"). changeColor (...); here, the changeColor is the object-based extension.

Use the $. fn. extend method when developing extension methods, that is, jQuery. fn. extend (object );

The Code is as follows:


$. Fn. extend ({

Check: function (){
Return this. each ({
This. checked = true;
});
},
Uncheck: function (){
Return this. each ({
This. checked = false;
});
}
});


Page call:

The Code is as follows:


$ ('Input [type = checkbox] '). check ();
$ ('Input [type = checkbox] '). uncheck ();


3. Expansion

The Code is as follows:


$. Xy = {
Add: function (a, B) {return a + B ;},
Minus: function (a, B) {return a-B ;},
VoidMethod: function () {alert ("void ");}
};
Var I = $. xy. add (3, 2 );
Var m = $. xy. minus (3, 2 );
$. Xy. voidMethod ();

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.