Share study notes of jQuery plug-in_jquery
Plugin is also called jQuery Extension, which is a program written according to a certain standard application interface. There are currently more than thousands of jQuery plug-ins, which are written, verified and improved by developers from all over the world. For jQuery developers, using these plug-ins directly will quickly stabilize the system architecture and save project costs.
1. Plugin Overview
The plug-in is based on the core code of jQuery to write an application that combines certain specifications. In other words, the plug-in is also jQuery code, which can be inserted through the introduction of js files.
There are many types of plug-ins, which can be roughly divided into: UI class, form and verification class, input class, special effects class, Ajax class, sliding class, graphics and image class, navigation class, comprehensive tool class, animation class, etc.
Introducing plug-ins requires certain steps, which are basically as follows:
- 1. The jquery.js file must be imported first, and before all plug-ins;
- 2. Introduce plug-ins;
-
3. Introduce plug-in peripherals, such as skins, Chinese language packs, etc.
For example, the most commonly used form validation plug-in: validate, in addition to the most basic plug-in file jquery.validate.min.js, there are also messages_zh.js and other language packages from various countries for you to use.
How to use this plug-in will not be described here. You can check out the video on MOOC.com about the jQuery plug-in - Validation Plugin, which describes in detail the various configuration items and extensions of this plug-in.
By analogy, for an excellent plug-in, detailed documentation is essential. Reading the documentation in detail and testing it locally will allow you to quickly get started using various plug-ins.
There are also various plug-ins written by others that you can use: such as plug-ins for managing cookies – cookies, etc.
Plug-ins can be found in the plug-in module on the jQuery official website. The jQuery Plugin Registry
2. Custom plug-in
Earlier we used good plug-ins provided by others, which are very convenient to use. If you can't find a plug-in that you are satisfied with on the market, and want to package a plug-in yourself for others to use. Then you need to write a jQuery plug-in yourself.
1. Types of plug-ins
According to functional classification, plug-in forms can be divided into the following three categories:
- Plug-in that encapsulates object methods; (that is, a jQuery object based on a certain DOM element, localized, and most used)
- Plug-in that encapsulates global functions; (global encapsulation)
- Selector plugin. (Similar to .find(), for example: color(red) to select all red elements)
2. Basic points of plug-ins
Mainly used to solve various conflicts, errors and other problems caused by plug-ins, including the following:
- It is recommended to use jQuery.[plugin name].js for the plug-in name to avoid conflicts with other js files or other libraries (you can use alert( $.[plug-in name]) to verify whether the global method exists );
- The local object is attached to the jQuery.fn object, and the global function is attached to the jQuery object itself (you can use alert( $(selector).[plugin name]) to verify whether the local method exists);
- Inside the plug-in, this points to the current local object (jQuery object obtained through the selector);
- You can use this.each to traverse all elements;
- All methods or plug-ins must end with a semicolon to avoid problems (to be more secure, you can add a semicolon at the head of the plug-in);
- The plug-in should return a jQuery object to ensure chainable operations;
- Avoid using $ inside the plug-in. If you want to use it, use a closure to pass jQuery in, so that the plug-in will continue to use $ as an alias for jQuery.
;(function($){//这里将$符作为匿名函数的形参 /*在此处编写代码,可使用$作为jQuery的缩写别名*/ })(jQuery);//这里将jQuery作为实参传递给匿名函数了
3. Write a plug-in
Suppose our plug-in requirement is to implement a drop-down menu, display the corresponding drop-down list when the element is moved in, and retract it when it is moved out. And you can set the text color when it is expanded.
According to convention, when we write plug-ins, we can have some constraints on the html structure. Now assume that our page has the following HTML structure:
<ul class="list"> <li>导航列表1 <ul class="nav"> <li>导航列表1</li> <li>导航列表2</li> <li>导航列表3</li> <li>导航列表4</li> <li>导航列表5</li> <li>导航列表6</li> </ul> </li> <li>导航列表2 <ul class="nav"> <li>导航列表1</li> <li>导航列表2</li> <li>导航列表3</li> <li>导航列表4</li> <li>导航列表5</li> <li>导航列表6</li> </ul> </li> </ul> <!-- 默认已经引入jquery -->
在这里,我们就对我们的插件实现效果有了第一个要求,必须在 对于需要hover展现的元素 内部下面建立 ul 列表,且 className 必须为 nav 。(插件内部都是根据该条件来做文章)
下面就可以开始编写我们的插件了。保存为 jQuery.nav.js ,(符合上面所说的要求,默认已经导入)
;(function($){ $.extend({ //插件定义在全局方法上 "nav" : function (color){//传参,这里只是抛砖引玉,在您编写的时候,参数选项可以更加丰富,例如传入json对象等等 $('.nav').css({//对展开的下拉列表设置样式,此处在下面进行详细说明 "list-style" : "none", "margin" : 0, "padding" : 0, "display" : "none", "color":color//由用户控制hover时,展现出来列表的文字颜色 }); $('.nav').parent().hover(//这里用到了.nav的父节点(就是hover到的元素) //因为我们只能在插件要求的范围内进行设定,若是使用了外部的选择器,就违背了这个原则 function (){ $(this).find(".nav").stop().slideDown("normal");//注意我们在这里使用了jquery的动画方法 },function (){ $(this).find(".nav").stop().slideUp("normal");//注意stop()的使用,不然会有类似手风琴效果的出现,但那并不是我们需要的 }); } }); })(jQuery);
注意:这里使用css方法只是为了方便,在真实插件编写过程中,若存在如此大量的css样式编写时,推荐在外部定义css样式,例如可改写为:
插件依赖的css,需和插件一起导入html中
.hover{/*插件必须样式*/ list-style: none; margin:0; padding: 0; display: none; }
在插件内部修改。
$('.nav').addClass("hover");//将CSS与jQuery分离开来 $('.nav').css("color",color);//存在用户设置时启用,不存在就不用了(进行判断)
刚刚说的都是插件的js文件,最后要起到效果,别忘了页面的js中加上那么一句话(当前插件定义在全局方法上)
$(function (){ $.nav("#999");//调用插件实现的全局方法,并且设置其展现背景颜色为#999。 });
就这样,我们的全局插件就编写,而且调用完成了,在你的页面刷新看看,是不是已经有了效果呢?
但是,因为我们的方法定义在全局上,现在只要页面中出现了我们插件所规定的结构,就会存在hover展现效果,但是有时我们往往想要的不是这样,我们希望它在局部,在我指定的元素调用。所以我们需要对其进行一些改造,让其变成局部方法,其实也很简单:
;(function($){ $.fn.extend({//定义为局部方法 "nav" : function (color){ $(this).find('.nav').addClass('hover');//上面已经说过了,此时this指向调用该方法的元素 $(this).find('.nav').css("color",color);//在当前元素下,增加了一次find筛选,实现在对应的元素中执行。 $(this).find('.nav').parent().hover( function (){ $(this).find(".nav").stop().slideDown("normal"); },function (){ $(this).find(".nav").stop().slideUp("normal"); }); return this;//返回当前的对象 } }); })(jQuery);
我们去刷新一下浏览器。你会发现,咦,怎么没效果? 当然因为原来的代码是直接在全局调用的,现在变成局部方法了,显然就不能这样做了,需要做一点改变:
我这里就不贴html代码了,但是希望您在实践时能把上面的html代码在其下复制几份,以达到思考其实现的效果
$(function (){//这里的eq(0)代表只对第一份起到效果,复制后的没有效果。 //(你可以把上面的find筛选删除之后再试试,您会发现,他又对余下的几份起效果了) $(".list").eq(0).nav("red");//调用局部方法 });
现在回过头再把我们写的插件代码对应上面写的 插件编写的要点 ,思考一下,我们还有哪些没有做到?就会发现,基本已经能对应上了。现在我们就完成了一个下拉菜单的插件。
其实编写插件并不难,最主要的是在我们编写插件的时候,一定要时刻注意这样的要点,每一个细节都遵循在大家实践过程中总结出来的最佳实现,才能自定义实现一个良好的插件。
代码首先是写个人看的,再然后才是给机器看的。

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

AI Hentai Generator
Generate AI Hentai for free.

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



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.

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

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 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.

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
