How to understand Jquery plug-in
In actual development work, we will always encounter business needs such as scrolling, paging, calendar and other display effects. For those who have been exposed to jQuery
and are familiar with the use of jQuery
, the first thing that comes to mind is definitely to look for existing jQuery
plug-ins to meet the corresponding display needs. There are a variety of jQuery
plug-ins to choose from for some components commonly used in current pages. There are also many websites on the Internet that specifically collect jQuery
plug-ins. Using the jQuery
plug-in can indeed bring convenience to our development work, but if you only use it simply and don’t understand the principles, you will encounter problems during use or customize the development of the plug-in. There will be many doubts. The purpose of this article is to quickly understand the development principles of jQuery
plug-ins and master the basic skills of jQuery
development.
Before developing jQuery plug-ins, you must first know two questions: What is a jQuery plug-in? How to use jQuery plug-in?
The first question, jQuery
plug-in is a method used to extend jQuery
prototype object. Simply put, jQuery
plug-in isjQuery
A method of the object. In fact, after answering the first question, you will know the answer to the second question. The way to use the jQuery
plug-in is to call the jQuery
object method.
Let’s look at an example first: $("a").css("color","red")
. We know that each jQuery
object will contain the DOM
operation method defined in jQuery
. Here, the $ method is used to select the a element and return an ## of the a element. #jQuery object, this object can use the
DOM operation method defined in
jQuery. So how does the
jQuery object obtain these methods? In fact,
jQuery internally defines a
jQuery.fn object. Looking at the
jQuery source code, you can find
jQuery.fn=jQuery.prototype, that is It is said that the
jQuery.fn object is the prototype object of
jQuery, and the
DOM operation methods of
jQuery are all in
jQuery.fnDefined on the object, then the
jQuery object can inherit these methods through the prototype.
1. Basic jQuery plug-in
After knowing the above knowledge, we can write a simplejQuery plug-in. If I now need a
jQuery plug-in to change the color of the label content, I can implement the plug-in in the following way:
$.fn.changeStyle = function(colorStr){ this.css("color",colorStr); }
$("p").changeStyle("red");
jQuery object currently calling the plug-in. In this case, each tag selected using the
$() method will be called
changeStyle()When plug-in, the
css() method will be used to reset the
color style.
2. jQuery plug-in that satisfies chain call
Chain call is a major feature ofjQuery, a general plug-in should follow
jQuery style, meeting the requirements of chain calls. The way to implement chain calling is also very simple:
$.fn.changeStyle = function(colorStr){ this.css("color",colorStr); return this; }
$("p").changeStyle("red").addClass("red-color");
return this, this line of code is added to the plug-in, then after the plug-in is executed, the current
jQuery object will be returned, and then you can continue to call other
jQuery after the plug-in method method.
3. jQuery plug-in to prevent $ symbol pollution
There are many js libraries that use the$ symbol, although
jQuery You can use the
jQuery.noConflict() method to hand over the right to use the
$ symbol, but if you define a plug-in, use the
$.fn object to define it, Then when these plug-ins are used, they will be affected by other
js libraries that use
$ variables. In this case, we can use the immediate execution function to encapsulate the plug-in by passing parameters. The form is as follows:
(function($){ $.fn.changeStyle = function(colorStr){ this.css("color",colorStr); return this; } }(jQuery));
$ symbol can be avoided.
4. A jQuery plug-in that can accept parameters
Continuing with the above example, if I also want to add a function to this plug-in to set the text size of the label element content, then I can implement it like this:(function($){ $.fn.changeStyle = function(colorStr,fontSize){ this.css("color",colorStr).css("fontSize",fontSize+"px"); return this; } }(jQuery));
(function($){ $.fn.changeStyle = function(option){ this.css("color",option.colorStr).css("fontSize",option.fontSize+"px"); return this; } }(jQuery));
$("p").changeStyle({colorStr:"red",fontSize:14});
Put Another advantage of putting the parameters in an object and passing them to the plug-in is that we can define some default values for some parameters inside the plug-in, for example:
(function($){ $.fn.changeStyle = function(option){ var defaultSetting = { colorStr:"green",fontSize:12}; var setting = $.extend(defaultSetting,option); this.css("color",setting.colorStr).css("fontSize",setting.fontSize+"px"); return this; } }(jQuery));
上面的代码用到了$.extend
方法,这个方法在这里的用法就是合并两个对象,即把后面一个对象的存在的属性值赋值给第一个对象,具体用法可以参考这里。$.extend
方法还有一种作用是用来扩展jQuery
对象本身。
这样定义的插件,我们在使用时如果不传fontSize
,那么使用这个插件的jQuery
对象标签的内容会被设置成默认的12px
。
使用方式: $("p").changeStyle({colorStr:"red"});
注意:在为插件定义默认参数时,一定要把默认参数写在插件方法内部,这样默认参数的作用域就在插件内部。
总结
定义插件的方式除了上面说的用$.fn
来定义,还有另外一种方式来定义插件,那就是使用$.fn.extend
方法。类似下面的写法:
//注意为了更好的兼容性,开始前有个分号;(function($){ $.fn.extend({ changeStyle:function(option){ var defaultSetting = { colorStr:"green",fontSize:12}; var setting = $.extend(defaultSetting,option); this.css("color",setting.colorStr).css("fontSize",setting.fontSize+"px"); return this; } }); }(jQuery));//这里将Jquery作为实参传递给匿名函数
PS: $.extend
方法和$.fn.extend
方法都可以用来扩展jQuery
功能,通过阅读jQuery
源码我们可以发现这两个方法的本质区别,那就是$.extend
方法是在jQuery
全局对象上扩展方法,$.fn.extend
方法是在$选择符选择的jQuery
对象上扩展方法。所以扩展jQuery
的公共方法一般用$.extend
方法,定义插件一般用$.fn.extend
方法。
The above is the detailed content of How to understand Jquery plug-in. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When users use the Edge browser, they may add some plug-ins to meet more of their needs. But when adding a plug-in, it shows that this plug-in is not supported. How to solve this problem? Today, the editor will share with you three solutions. Come and try it. Method 1: Try using another browser. Method 2: The Flash Player on the browser may be out of date or missing, causing the plug-in to be unsupported. You can download the latest version from the official website. Method 3: Press the "Ctrl+Shift+Delete" keys at the same time. Click "Clear Data" and reopen the browser.

What is the Chrome plug-in extension installation directory? Under normal circumstances, the default installation directory of Chrome plug-in extensions is as follows: 1. The default installation directory location of chrome plug-ins in windowsxp: C:\DocumentsandSettings\username\LocalSettings\ApplicationData\Google\Chrome\UserData\Default\Extensions2. chrome in windows7 The default installation directory location of the plug-in: C:\Users\username\AppData\Local\Google\Chrome\User

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How does Google Chrome allow animation plugins to run? Google Chrome is very powerful. Many friends like to use this browser to watch video animations. However, if you want to watch various animated videos, you need to install animation plug-ins in the browser. Many friends use Google Chrome. After installing the animation plug-in, I still cannot care about the video. How should I deal with this problem? Next, let the editor show you the specific steps to allow the animation plug-in to run in Google Chrome. Friends who are interested can come and take a look. Specific steps for Google Chrome to allow animation plug-ins to run: 1. First run Google Chrome on your computer, and click the main menu button in the upper right corner of the homepage (as shown in the picture). 2. After opening the main menu, select the "Settings" option below (as shown in the picture). 3. In settings

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to unblock the Google Chrome plug-in? Many users like to install various useful plug-ins when using Google Chrome. These plug-ins can provide rich functions and services and improve work efficiency. However, some users say that after installing plug-ins in Google Chrome, the plug-ins will always be displayed. is blocked, so how can you unblock the plug-in after encountering this situation? Now let the editor show you the steps to unblock plug-ins in Google Chrome. Friends in need should come and take a look. How to unblock plug-ins in Google Chrome Step 1. When the blocked prompt appears, click the "Control Bar" and select "Install ActiveX Control". 2. Then open the browser "Tools" menu and click "Internet Options". 3.
