Popularize JQuery knowledge
Knowledge 1: When writing plug-ins with JQuery, the core methods are as follows:
$.fn.extend(object) can be understood as adding a method to the JQuery instance.
Basic definition and call:
Knowledge 2: The difference between jQuery(function () { }); and (function ($) { })(jQuery);:
jQuery(function () { }); is the code in the execution method after a DOM element is loaded.
(function ($) { })(jQuery); defines an anonymous function, where jQuery represents the actual parameters of this anonymous function. Usually used in JQuery plug-in development, it plays the role of defining the private domain of the plug-in.
Three: Develop JQuery plug-in standard structure
1. Define the scope: To define a JQuery plug-in, you must first place the code of the plug-in in a place that is free from external interference. To put it in more professional terms, you need to define a private scope for this plug-in. External code cannot directly access the code inside the plug-in. Code inside the plug-in does not pollute global variables. To a certain extent, it decouples the dependence between plug-ins and the running environment. Having said all that, how do you define the private scope of a plug-in?
Up to now, one of the simplest JQuery plug-ins has been completed. When calling, you can call this plug-in in $("#domName").easySlider({}), or $(".domName").easySlider({}) or more.
3. Set default value: Define a JQuery plug-in, just like defining a .net control. A perfect plug-in should have relatively flexible properties. Let's look at this code:
Programmers like to innovate, such as changing variable names, changing lines, etc. When I saw var defaults = {} used to represent a default attribute, I thought about being different when I wrote the JQuery plug-in, so I used var default01 ={} and var default02 ={} to represent the default attribute. Then the default attribute names are all kinds and getting worse. Therefore, it is recommended that when writing a JQuery plug-in, when defining default properties, use the defaults variable to represent the default properties. Such code is more readable.
Someone saw this line of code: var options = $.extend(defaults, options) and frowned, expressing confusion. Then let’s look at the following code first:
var a = $.extend(obj01, obj02);
var b = $.extend(true, obj01, obj02);
var c = $.extend({}, obj01, obj02);
var d = $.extend(true,{}, obj01, obj02);
Copy the code to the development environment, look at the values of a, b, c, and d respectively, and you will understand the meaning of var options = $.extend(defaults, options). Indicates that options overrides the value of defaults and assigns the value to options.
In the plug-in environment, it means that the value set by the user overrides the default value of the plug-in; if the user does not set the attribute with a default value, the default value of the plug-in is still retained.
4. Support JQuery selector: JQuery selector is an excellent feature of JQuery. If our plug-in is written not to support JQuery selector, it is indeed a big regret. In order to make our JQuery plug-in support multiple selectors, our code should be defined like this:
5. Support JQuery link calling: The above code seems perfect, but in fact it is not so perfect. Link calls are not supported so far. In order to achieve the effect of link calling, each element of the loop must be returned
Such a definition can support link calls. For example, it supports calls like this: $(".div").easySlider({prevId:"",prevText:""}).css({ "border-width": "1", "border-color": "red ", "border-bottom-style": "dotted" });
6. Methods in plug-ins: Implementing the functions of a plug-in often requires a large amount of code, which may be hundreds, thousands, or even tens of thousands of lines. We need to use functions to structure this code. As mentioned in the first point, the methods defined in the plug-in cannot be directly called by the outside world. The methods I defined in the plug-in do not pollute the external environment. Now try to define some methods in the plug-in:
//step03-b merge user-defined attributes, default attributes
var options = $.extend (defaults, options);
//step4 supports JQuery selector
//step5 supports chain call
return this.each(function () {
//step06-b is defined in the plug-in Method
showLink(this);
});
Step06-a: A method called showLink() is defined in the plug-in; this method cannot be called directly outside the plug-in. It is a bit like a private method in a C# class and can only be used within the plug-in. Step step06-b demonstrates how to call methods inside the plug-in.