jquery Plug-in Production Simple example Description _jquery

Source: Internet
Author: User
Tags extend
First, from a simple example, do not need a method with parameters to start
Copy Code code as follows:

Create an anonymous function
(function ($) {
Attach a new method to jquery (see note 1 for details)
$.fn.extend ({
Name of the plugin
Myfirstname:function () {
Iteration Current matching element collection
Return This.each (function () {
var obj = $ (this);
Your own code
});
}
});
) (JQuery);

Note 1: To understand the difference between $.fn.extend and $.extend, presumably the former incorporates the MyFirstName method into the instance object of jquery, such as $ ("#txtmy"). Add (3,4) calls methods like this, The latter is merging the MyFirstName method into the global object of jquery, such as $.add (3,4); This calls the method
Detailed distinction See (http://www.jb51.net/article/29590.htm)

Two, have the parameter of
Copy Code code as follows:

Create an anonymous function
(function ($) {
Attach a new method to jquery (see note 1 for details)
$.fn.extend ({
Name of the plugin
Myfirstname:function () {
Defining Default Parameters
Var parms={
Parms1:1,
Parms2:2
}
The parameters and default parameters passed by the merged user are returned to the options (see note 2 for details)
var options = $.extend (defaults, options);
Iteration Current matching element collection
Return This.each (function () {
Assign the merged parameters to O
var o= options;
Iterate the current matching element
var obj = $ (this);
Your own code
});
}
});
) (JQuery);

Remark 2:var options = $.extend (defaults, options); It means merging defaults and options, if the latter has the same element as the former name, the latter covering the former, then merging to defaults, then defaults assigning to options, if the var options = $.extend ({}, defaults, options); then combine the former with the latter to {} This parameter, and then assign the value to the options,defaluts structure and values unchanged
Detailed distinction See (http://www.jb51.net/article/29591.htm)

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.