So first, let’s take a brief look at the most orthodox jQuery plug-in definition method:
(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):
})(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.
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