One, jquery expansion
1, $.extend (object)
Similar to. NET extension method for extending jquery. It can then be called in the form of $.
$ (function () {$.extend () { fun1:function () {alert ("Extend a fun1 function for jquery!");}); $.fun1 (); })
2, $.fn.extend (object)
The object that extends jquery.
$.fn.extend ({fun2:function () {Alert ("execution Method 2");}}); $ ("#id1"). Fun2 ();
You can use Google to see:
The above notation is equivalent to:
$.fn.fun2 = function () {Alert ("execution Method 2"); $ (this). FUN2 ();
Second, private domain
It is defined in the following way:
(function ($) {}) (jquery);//equivalent to var fn = function (xxoo) {};FN (jquery);
The following code pops up 123.
$ (function () { var fn = function (Xxoo) {}; fn (alert (123)); })
Iii. basic steps for defining plug-ins
1. Define Scope
The development of a jquery plug-in, the first to isolate the plug-in code with the outside world, the external code does not allow direct access to the plug-in internal code, plug-in internal code does not affect the external.
Step 1 defines the plug-in private scope (function ($) { }) (JQuery);
This will ensure that the code inside the plugin is isolated from the outside world.
2. Extending jquery
After defining the scope, you need to extend the plug-in to jquery in order for the external calls to be made.
Step 1 Define the private scope (function ($) { ///Step 2 plug-in extension method name $.fn. MyFrame = function (options) { } }) (JQuery);
3. Default value
After you have defined the jquery plug-in, you can specify it in this way if you want some parameters to have default values.
Step 1 Define the private scope (function ($) { //Step 3 Plugin Default Value property var defaults = { Id: ' #id1 ', }; Step 2 Plug-in extension method name $.fn. MyFrame = function (options) { //Step 3 Merge user-defined properties, default properties (if options are empty, use defaults) var options = $.extend (Defaults, options); } ) (JQuery);
4. Support jquery Selector
Step 1 Define the private scope (function ($) { //Step 3 Plugin Default Value property var defaults = { Id: ' #id1 ', }; Step 2 Plug-in extension method name $.fn. MyFrame = function (options) { //Step 3 Merge user-defined properties, default properties (if options are empty, use defaults) var options = $.extend (defaults , options); } Step 4 supports jquery selector This.each (function () { }); }) (JQuery);
5, support the chain Call of jquery
Step 1 Define the private scope (function ($) { //Step 3 Plugin Default Value property var defaults = { Id: ' #id1 ', }; Step 2 Plug-in extension method name $.fn. MyFrame = function (options) { //Step 3 Merge user-defined properties, default properties (if options are empty, use defaults) var options = $.extend (defaults , options); } Step 4 Support jquery selector //Step 5 Support chaining call (return step 4) return This.each (function () { }); }) (JQuery);
6, plug-in internal methods
Step 1 Define the private scope (function ($) { //Step 3 Plugin Default Value property var defaults = { Id: ' #id1 ', }; Step 6 defines the function in the plugin var myfun = function (obj) { alert (obj); } Step 2 Plug-in extension method name $.fn. MyFrame = function (options) { //Step 3 Merge user-defined properties, default properties (if options are empty, use defaults) var options = $.extend (defaults , options); } Step 4 support for jquery selector //Step 5 support chaining calls (returning step 4) return This.each (function () { //step 6 define functions myfun in the plugin (this) ; }); }) (JQuery);
Because of the scope relationships, the private functions of step 6 are currently allowed for internal use by plug-ins.
This article is a note, originally from: http://www.cnblogs.com/xcj26/p/3345556.html
jquery Plugin Development method