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)