JQuery plugin Development Guide and jquery Development Guide

Source: Internet
Author: User

JQuery plugin Development Guide and jquery Development Guide

First, let's take a simple look at the most orthodox jQuery plug-in definition:

Copy codeThe Code is as follows:
(Function ($ ){
$. Fn. Plug-in name = function (settings ){
// Default parameter
Var defaultSettings = {
 
}/* Merge the default parameters and user-defined parameters */
 
Settings = $. extend (defaultSettings, settings );
 
Return this. each (function () {// code}); // The plug-in appears multiple times within the element
 
}) (JQuery );

 
First, let's look at the first line of code in the template (of course we need to pull out the second half of this line of code, otherwise the first line will be completely meaningless ):

Copy codeThe Code is as follows:
(Function ($ ){
 
}) (JQuery );

This line of code is actually used to create an anonymous function. If you do not know about anonymous functions and closures, you will be very confused about this code. Therefore, we strongly recommend that you read [Details about JavaScript anonymous functions and closures.

JQuery's Inheritance Method $. extend -- $. extend plays an important role in jQuery plug-in development and is used to merge parameters.

Copy codeThe Code is as follows:
$. Fn. tip = function (settings ){
Var defaultSettings = {
// Color
Color: 'yellow ',
// Latency
Timeout: 200}
/* Merge the default parameters and user-defined parameters */
Settings = $. extend (defaultSettings, settings );
Alert (settings. input); <br>}

JQuery plugin defines the second method:

Copy codeThe Code is as follows:
(Function ($ ){
// Plug-in definition -- change the name
$. Fn. tabpanel = function (method ){
Var methods = $. fn. tabpanel. methods;
If (methods [method]) {
Return methods [method]. apply (this, Array. prototype. slice. call (arguments, 1 ));
} Else if (typeof method = 'object' |! Method ){
Return methods. init. apply (this, arguments );
} Else {
 
}
}
// Supported Methods
$. Fn. tabpanel. methods =
{
// Initialization
Init: function (p_options ){
TabpanelBind (p_options, this );
},
Add: function (p_options ){
AddTab (p_options, this );
TabpanelBind (p_options, this );
// Debugger
}}
Function add (p_options ){
Var _ defaults = {
Id :""
}
<Br> // internal implementation ...... <br> return _ index;
}
<Br >}) (jQuery); <br> call $ ("# team"). tabpanel ('add ',"");

All right, the two development methods described above are the most common. Let's take a good look at them. We will introduce them more deeply later.

Related Article

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.