Share about jQuery plug-in examples
Classification of jquery plug-ins
jQuery
There are many plug-ins, including UI
classes, Form validation, input classes, special effects classes,Ajax
classes, sliding classes, navigation classes, tool classes, animation classes, etc.
jQuery
plug-ins are mainly divided into three categories:
1、封装对象方法的插件:也就是基于某个DOM元素的jQuery对象,局部性 2、封装全局函数的插件:将独立的函数添加到jquery的命名空间之下。jquery.trim()就是jquery内部作为全局函数的插件添加到内核上去的。 3、选择器插件:扩充自己喜欢的一些选择器。类似于.toggle()
Basic points of plug-ins
1. 插件的文件名推荐为 jquery.[插件名].js,例如jquery.validate.js 2. 所有的对象方法都应当附加到jQuery.fn对象上,而所有的全局函数都应当附加到jQuery对象本身上 3. 在插件内部,this指向的是当前通过选择器获取的jQuery对象,而不像一般的方法那样,例如click()方法,内部的this指向的是DOM元素 4. 可以通过this.each来遍历所有元素 5. 所有的方法或函数插件,都应当以分号结尾,否则压缩时可能出问题,有的为了更加稳妥些,在插件的开始处加上一个分号 6. 插件应该返回一个jQuery对象,这样可以保证插件的可链式操作。除非插件需要返回的是一些需要获取的量,例如字符串或者数组等 7. 尽量利用闭包技巧历来避免变量名的冲突 8. 引入的自定义插件必须在jQuery库后面
Plug-in development methods
jQuery
There are three main methods of plug-in development:
通过$.extend()来扩展jQuery 通过$.fn 向jQuery添加新的方法 通过$.widget()应用jquery ui的部件工厂方式创建
Usually we use the second method for simple plug-in development, and the first method is to add in the jQuery
namespace A static method does not allow us to select a DOM element and then call the method. The third type is not commonly used and is more complicated.
The structure of the plug-in
When starting to write the jQuery
plug-in, we need to understand the basic structure of the plug-in. All The jQuery
plugins are declared as methods of the jQuery.fn
object:
jQuery.fn.showPlugin = function () { //code here };
We use the following code to use the plugin:
$("#plugin").showPlugin();
Here, I do $
is not used. This is to avoid naming conflicts. If you want to use $
, you can encapsulate the plug-in code in a self-executing anonymous function as follows , and then pass in the parameters jQuery
(function($){ jQuery.fn.showPlugin = function () { //code here }; })(jQUery);
$.extend
.extend()
allows you to extend the base object using one or more objects , the way of expansion is to merge the objects on the right to the base object (the first object on the left) in sequence.
;(function($){ $.extend({ 'nav' : function () { } }) })(jQuery);
We use this global method as follows:
$.nav();
Earlier (see: A brief analysis of the overall jQuery framework and implementation (Part 1)) we said, $.extend
is global, and $.fn.extend
is local. The reason why a semicolon is added in front is to prevent the js
file introduced before from not adding points. The number
$.fn.myPlugin = function() { //在这里面,this指的是用jQuery选中的元素 //example :$('a'),则this=$('a') this.css('color', 'red'); }
this
refers to the collection returned by the jQuery
selector. this
in the plug-in has undergone some encapsulation processing, and this
represents the jQuery
object. There is no need to use $()
for packaging again
Chain call
To make the plug-in implement chain call, you only need to return
the object That's it:
$.fn.myPlugin = function() { this.css('color', 'red'); return this.each(function() { //对每个元素进行操作 $(this).append(' ' + $(this).attr('href')); })) }
Use return
this.each(function () {}
This realizes our chain operation.
Receive When calling multiple parameters
$.fn.myPlugin = function(options) { //经常用options表示有多个参数。 var defaults = { 'color': 'red', 'fontSize': '12px' }; var settings = $.extend(defaults, options); return this.css({ 'color': settings.color, 'fontSize': settings.fontSize }); }
, the font size will use the default value in the plug-in:
$('a').myPlugin({ 'color': '#2C9929' });
The above is the detailed content of Share about jQuery plug-in examples. 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



Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the "Share to" option at the bottom, and then select the first "WeChat Moments" allows you to share content to WeChat Moments.

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

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.

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the "Shared Members" column 】, and finally check all

Title: To solve the problem that Discuz WeChat shares cannot be displayed, specific code examples are needed. With the development of the mobile Internet, WeChat has become an indispensable part of people's daily lives. In website development, in order to improve user experience and expand website exposure, many websites will integrate WeChat sharing functions, allowing users to easily share website content to Moments or WeChat groups. However, sometimes when using open source forum systems such as Discuz, you will encounter the problem that WeChat shares cannot be displayed, which brings certain difficulties to the user experience.

HP printers are essential printing equipment in many offices. Installing the printer driver on the computer can perfectly solve problems such as the printer being unable to connect. So how to install HP printer driver? The editor below will introduce you to two HP printer driver installation methods. The first method: download the driver from the official website 1. Search the HP China official website in the search engine, and in the support column, select [Software and Drivers]. 2. Select the [Printer] category, enter your printer model in the search box, and click [Submit] to find your printer driver. 3. Select the corresponding printer according to your computer system. For win10, select the driver for win10 system. 4. After downloading successfully, find it in the folder

People in the workplace will be familiar with PPT production, because whether it is a year-end summary or a work report, many companies require it to be presented in the form of PPT. At this time, I encountered a problem, that is, how to share PPT? Don’t worry, the editor below will show you how to share PPT. 1. First select the edited PPT and click Save in the upper left corner (if you are using WPS, you can click Login first). 2. Then click the share icon in the menu bar as shown below. 3. Then the sharing interface as shown below will pop up. You can see that a sharing link will appear. Click to send the link to share. 4. You can also click "Allow friends to edit" in the lower left corner of the picture below, so that friends can also click to edit this PPT. 5. If necessary, give P
