Home > php教程 > PHP开发 > Detailed explanation of jQuery plug-in development methods

Detailed explanation of jQuery plug-in development methods

高洛峰
Release: 2016-12-05 16:06:10
Original
1733 people have browsed it

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();
})
Copy after login

  2. $.fn.extend(object)

Extend jQuery object.

$.fn.extend({ fun2: function () { alert("执行方法2"); } });
$("#id1").fun2();
Copy after login

You can use google to see:

Detailed explanation of jQuery plug-in development methods

The above writing is equivalent to:

$.fn.fun2 = function () { alert("执行方法2"); }
$(this).fun2();
Copy after login

2. Private domain

It is defined as follows:

(function ($) { })(jQuery);
//相当于
var fn = function (xxoo) { };
fn(jQuery);
Copy after login

The following code pops up 123.

$(function(){
var fn = function (xxoo) { };
fn(alert(123));
})
Copy after login

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);
Copy after login

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);
Copy after login

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);
Copy after login

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);
Copy after login

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);
Copy after login

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);
Copy after login

Due to scope Relationships, the private functions of step 6 are currently allowed for internal use by plugins.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template