Home >Web Front-end >JS Tutorial >jQuery plug-in development guide_jquery

jQuery plug-in development guide_jquery

WBOY
WBOYOriginal
2016-05-16 16:31:221720browse

So first, let’s take a brief look at the most orthodox jQuery plug-in definition method:

Copy code The code is as follows:

(function ($) { 
$.fn.Plug-in name = function (settings) {                                                //Default parameters 
var defaultSettings = {

                                                                                                                                                                                                                                                                                      /* Merge default parameters and user-defined parameters
settings = $.extend(defaultSettings, settings);

return this.each(function () {                                                                                                                                                                                                                                                                                              
} })(jQuery);



Let’s first look at the first line of code in the template (of course we have to pull out the second half of this line of code and read it together, otherwise the first line will be completely meaningless):


Copy code The code is as follows: (function ($) {
})(jQuery);



This line of code is actually used to create an anonymous function. If you don’t understand anonymous functions and closures, you will be very confused about this kind of code, so it is strongly recommended that you read this article [
Talk about JavaScript anonymous functions and closures in detail].

jQuery’s inheritance method $.extend —— $.extend plays a very important role in jQuery plug-in development, which is used to merge parameters.

Copy code The code is as follows: $.fn.tip = function (settings) { 
var defaultSettings = {                                              //Color 
color: 'yellow', 
//delay
timeout: 200 }
/* Merge default parameters and user-defined parameters */
settings = $.extend(defaultSettings, settings);
alert(settings.input);
}



jQuery plugin defines the second way:

Copy code

Statement:
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