jQuery plug-in development
Generally speaking, the development of jQuery plug-ins is divided into two types: one is the global function hanging under the jQuery namespace, which can also be called a static method; the other is the jQuery object level method, that is A method that is hung under the jQuery prototype so that jQuery object instances obtained through the selector can also share this method.
1. jQuery extension
1. $.extend(object)
Similar to the .Net extension method, used to extend jQuery. Then you can call it using $..
$(function(){ $.extend({ fun1: function () { alert("为jQuery扩展一个fun1函数!"); } }); $.fun1(); })
2. $.fn.extend(object)
Extend jQuery object.
$.fn.extend({ fun2: function () { alert("执行方法2"); } }); $("#id1").fun2();
You can use google to see:
The above writing is equivalent to:
$.fn.fun2 = function () { alert("执行方法2"); } $(this).fun2();
2. Private domain
It is defined as follows:
(function ($) { })(jQuery); //相当于 var fn = function (xxoo) { }; fn(jQuery);
The following code pops up 123.
$(function(){ var fn = function (xxoo) { }; fn(alert(123)); })
3. Basic steps to define a plug-in
1. Define the scope
When developing a jQuery plug-in, you must first isolate the plug-in code from the outside world. External code is not allowed to directly access the internal content of the plug-in. Code, the code inside the plug-in does not affect the outside.
//Step 1 Define the plug-in private scope
(function ($) { })(jQuery);
This will ensure that the code inside the plug-in is isolated from the outside world.
2. Extend jQuery
After defining the scope, in order to allow external calls, you need to extend the plug-in to jQuery.
//步骤1 定义私有作用域 (function ($) { //步骤2 插件的扩展方法名称 $.fn.MyFrame = function (options) { } })(jQuery);
3. Default value
After defining the jQuery plug-in, if you want some parameters to have default values, you can specify them in this way.
//步骤1 定义私有作用域 (function ($) { //步骤3 插件的默认值属性 var defaults = { Id: '#id1', }; //步骤2 插件的扩展方法名称 $.fn.MyFrame = function (options) { //步骤3 合并用户自定义属性,默认属性(如果options为空,则使用defaults) var options = $.extend(defaults, options); } })(jQuery);
4. Support jQuery selector
//步骤1 定义私有作用域 (function ($) { //步骤3 插件的默认值属性 var defaults = { Id: '#id1', }; //步骤2 插件的扩展方法名称 $.fn.MyFrame = function (options) { //步骤3 合并用户自定义属性,默认属性(如果options为空,则使用defaults) var options = $.extend(defaults, options); } //步骤4 支持jQuery选择器 this.each(function () { }); })(jQuery);
5. Support jQuery chain call
//步骤1 定义私有作用域 (function ($) { //步骤3 插件的默认值属性 var defaults = { Id: '#id1', }; //步骤2 插件的扩展方法名称 $.fn.MyFrame = function (options) { //步骤3 合并用户自定义属性,默认属性(如果options为空,则使用defaults) var options = $.extend(defaults, options); } //步骤4 支持jQuery选择器 //步骤5 支持链式调用(将步骤4返回) return this.each(function () { }); })(jQuery);
6. Plug-in internal methods
//步骤1 定义私有作用域 (function ($) { //步骤3 插件的默认值属性 var defaults = { Id: '#id1', }; //步骤6 在插件里定义函数 var MyFun = function (obj) { alert(obj); } //步骤2 插件的扩展方法名称 $.fn.MyFrame = function (options) { //步骤3 合并用户自定义属性,默认属性(如果options为空,则使用defaults) var options = $.extend(defaults, options); } //步骤4 支持jQuery选择器 //步骤5 支持链式调用(将步骤4返回) return this.each(function () { //步骤6 在插件里定义函数 MyFun(this); }); })(jQuery);
Due to scope Relationships, the private functions of step 6 are currently allowed for internal use by plugins.