如何创建一个基础插件

How to Create a Basic Plugin

  有时你想使一块功能性的代码在你代码的任何地方有效.比如,也许你想调用jQuery对象的一个方法,对该对象进行一系列的操作.可能你写了一个真正有用的实用函数,想它能够轻易的移植到其他项目.在这种情况下,你可能想写一个插件.

  Sometimes you want to make a piece of functionality available throughout your code. For example, perhaps you want a single method you can call on a jQuery selection that perfroms a series of operations on the selection. Maybe you wrote a really useful utility function that you want to be able to move easily to other projects. In this case, you may want to write a plugin.

jQuery是如何工作的 101:jQuery 对象方法和实用方法

How jQuery Works 101:jQuery Object Methods and Utility Methods

  在我们写我们自己的插件之前, 我们必须先对jQuery如何工作有一些了解.请看看这段代码:

  Before we write our own plugins, we must first understand a little about how jQuery works.Take a look at this code:

$("a").css("color", "red");

  这是一段简单的jQuery代码,但是你知道在幕后发生了什么吗?每当你使用 $ 函数来选择元素,他会返回一个jQuery对象.这个对象包含了所有你以前使用过的方法(.css(), .click() 等等)和适合你的选择器的所有元素.jQuery对象从 $.fn 对象获取这些这些方法.这个对象包含了所有jQuery对象的方法,如果我们想写我们自己的方法,它也必须包含在内.

  This is some pretty basic jQuery code, but do you know what's happning behind the scenes?Whenever you use the $ function to select elements, it returns a jQuery object. This object contains all of the methods you've been using(.css(), .click(), etc.)and all of the elements that fit your selector. The jQuery object gets these methods from the $.fn object. This object contains all of the jQuery object methods, and if we want to write our own methods, it will need to contain those as well.

  此外jQuery实用函数 $.trim() 用来删除用户输入前后的空白符.实用方法是直接属于 $ 函数本身的.当jQuery API没有对你检索到的DOM元素组实现预期功能时,你也许有时想写一个实用函数插件对其进行扩展.

  Additionally the jQuery utility method $.trim() it used above to remove any leading or trailing empty space characters from the user input. Utility methods are functions that reside directly in the $ function itself. You may occasionally want to write a utility method plugin when your extension to the jQuery API does not have to do something to a set of DOM elements you've retrieved.

基本插件创作

Basic Plugin Authoring

  比如说我们想创建一个使检索到的元素变绿的插件.我们要做的就是在 $.fn 中添加一个名为 greenify 的函数,它会像其他jQuery对象函数一样有效.

  Let's say we want to create a plugin that makes test within a set of retrieved elements green. All we have to do is add a function called greenify to $.fn and it will be available just like any other jQuery object method.

$.fn.greenify = function(){
this.css("color", "green");
}
$("a").greenify();

注意使用 .css() 和其他类似方法时,我们用 this 而不是 $(this).这是因为我们的 greenify 函数和 .css() 是同一个对象的一部分.

Notice that use .css(), another method, we use this, not $(this). This is because our greenify function is a part of the same object as .css().

连贯性

Chaining

  它工作了,但是实际上我们会有几件事情要做,jQuery的特性之一就是连贯性,当你链接了四到五个动作到一个选择器.这是通过所有的 jQuery 对象方法再次返回源 jQuery 对象来实现的(这里有一些例外: 无参数调用 .width() 返回选中元素的宽度, 它是没有连贯性的).使我们的插件方法具有连贯性需要一行代码:

  This works, but there's a couple of things we need to do for our plugin to survive in the reeal world. One of jQuery's features is chaining, when you link five or six actions onto one selector. This is accomplished by having all jQuery object methods return the original jQuery object again(there are few exceptions: .width() called without parameters return the width of the selected element, and is not chainable). Making our plugin method chainable takes one line of code:

$.fn.greenify = function(){this.css("color", "green");return this;
}
$("a").greenify().addClass("greenified");

  值得注意的是连贯性的概念在类似 $.trim() 的 jQuery 实用函数上不适用.

  Note that the notion of chaining is not applicable to jQuery utility methods like $.trim().

保护$别名和添加范围

Protecting $ Alias and Adding Scope

  变量$在JavaScript库中非常受欢迎,如果你在使用jQuery的同时使用其他的库,你应使用jQuery.noConflict()来使jQuery不使用$.然而,假设$是jQuery函数的别名将损坏我们的插件.我们需要将我们所有的代码放进一个立即调用函数表达式里面, 然后传递函数jQuery,命名为参数$:

  The $ variable is very popular among JavaScript libraries, and if you're using another library with jQuery, you will have make jQuery not use the $ with jQuery.noConflict(). However, this will break our plugin since it is written with the assumption that $ is an alias to the jQuery function. To work well with other plugins, and still use the jQuery $ alias, we need to put all of our code inside of an Immediately Invoked Function Expression, and then pass the function jQuery, and name the parameter $:

(funtion($){$.fn.greenify = function(){this.css("color", "green");return this;}$.ltrim = function(str){return str.replace(/^\s+/, "");}$.rtrim = function(str){return str.replace(/\s+$/, "");}
}(jQuery));

  并且,立即调用函数表达式的主要作用是允许我们拥有自己的私有变量.假设我们想添加一个不同的颜色,而且我们想将它保存在一个变量里面.

  in addition, the primary purpose of an Immediately Invoked Function is to allow us to have our private variables. Pretend we want a different color, and we want to store it in a variable.

(function($){var shade = "#556b2f";$.fn.greenify = function(){this.css("color", shade);return this;}
}(jQuery));

Minimizing Plugin Footprint

  好的惯例是写一个插件只占用 $.fn 的一个接口,这段代码可能导致你的插件被其他的插件覆盖,也可能导致你的插件覆盖了其他插件,换句话说,这是段糟糕的代码:

  It's good practice when writing plugins to only take up one slot within $.fn. This reduces both the chance that your plugin will be overridden, and the chance that your plugin will override other plugins. In other words, this is bad:

(function($){$.fn.openpopup = function(){//open popup code.}$.fn.closepopup = function(){//close popup code.}
}(jQuery));

  使用单接口和使用参数来控制单接口执行什么样的行动将会好得多

  It would be much better to have one slot, and use parameters to control what action that one slot performs.

(function( $ ) {$.fn.popup = function( action ) {if ( action === "open") {// Open popup code.}if ( action === "close" ) {// Close popup code.}};
}( jQuery ));

使用 each() 方法

Using the each() Method

  你的典型的jQuery对象可能包含任意数量的DOM元素,这也是为什么经常把jQuery对象称为集合的原因.如果你想对特定的元素进行一些操作(例如:读取属性, 计算特定位置),那么你需要使用 .each() 来遍历这些元素.

  Your typical jQuery object will contain references to any number of DOM elements, and that's why jQuery objects are often referred to as collections. If you want to do any manipulating with specific elements (e.g. getting a data attribute, calculating specific positions) then you need to use .each() to loop through the elements.

$.fn.myNewPlugin = function(){return this.each(function(){//Do something to each element here.});
}

  注意我们返回 .each() 代替返回 this. 因为 .each() 已经是具有连贯性的,它的返回值是 this.在我们曾经做过的事中,到目前为止,这是一个维护可连续性更好的办法.

  Notice that we return the results of .each() instead of returning this. Since .each() is already chainable, it returns this, which we then return. This is a better way to maintain chainability than what we've been doing so far.

接受选项

Accepting Options

  当你的插件越来越复杂,好的主意是通过接受一些选项来使你的插件可定制.最简单的做到这一点的办法是一个字面对象,特别是有很多的选项.让我们修改我们的greenify插件以接受一些选项.

  As your plugins get more and more complex, it's a good idea to make your plugin customizable by accepting options. The easiest way do this, especially if there are lots of options, is with an object literal. Let's change our greenify plugin to accept some options.

(function($){$.fn.greenify = function(options){//This is the easiest way to have default optionvar settings = $.extend({//These are the defaults.color: "#55b62f",backgroundColor: "white"}, options);//Greenify the collection based on the settingsreturn this.css({color: setting.color,backgroundColor: setting.backgroundColor});};
}(jQuery));//Example usage:$("div").greenify({color: "orange"
});

  color 的缺省值 #556b2f 被 $.extend() 覆盖为 orange.

  The default value for color of #556b2f gets overridden by $.extend() to be orange.

把它们放在一起

Putting It Together

  这里是一个使用了一些我们已经讨论过的技巧的小插件例子:

Here's an example of a small plugin using some of the techniques we've discussed:

(function($){$.fn.showLinkLocation = function(){return this.filter("a").each(function(){$(this).append(" (" + $(this).attr("href") + ")");});};
}(jQuery));//Example usage:$("a").showLinkLocation();

  这个易用的插件遍历集合中所有的链接然后把href属性放在括弧中添加在元素后面.

  This handy plugin goes through all anchors in the collection and appends the href attribute in brackets.

  我们的插件可以通过以下代码优化:

  Our plugin can be optimized though:

(function($){$.fn.showLinkLocation = function(){return this.filter("a").append(function(){return " (" + this.href + ")";})};
}(jQuery));

  我们使用的是 .append() 方法的接受回调函数的能力,返回值将决定集合中的每个元素的尾部添加什么.注意我们不是使用 .attr() 方法来检索href属性,因为原生的DOM API给了我们适当命名的href属性方便使用.

  We're using the .append() method's capability to accept a callback, and the return value of that callback will determine what is appended to each element in the collection. Notice also that we're not using the .attr() method to retrieve the href attribute, because the native DOM API gives us easy access with the aptly named href property.

转载于:https://www.cnblogs.com/brokenjar/p/basic-plugin-creation.html

如何创建一个基础jQuery插件相关推荐

  1. 如何创建一个基本JQuery的插件

    如何创建一个基本的插件 有时您希望在整个代码中提供一些功能.例如,也许你想要一个单一的方法,你可以调用一个jQuery选择,对选择执行一系列的操作.在这种情况下,您可能需要编写一个插件. 链接jQue ...

  2. UE4-如何做一个简单的TPS角色(一)-创建一个基础角色

    打开UE4进入创建项目界面,选择游戏,第三人称项目进入下一步 按照以下配置创建一个项目,命名任意,一定要包含初学者内容!!!关系到后面素材使用 项目创建完成后,先创建一个Level文件夹,在文件夹中创 ...

  3. 【Django快速开发实战】(1~29)使用Django创建一个基础应用:职位管理系统

    1.总体描述 1.1产品需求: 1.2职位管理系统-建模 1.3 Django项目代码结构 新增recruitment项目 django-admin startproject recruitment ...

  4. [K/3Cloud] 创建一个单据转换插件

    概念: 创建一个业务单据转换插件,在单据转换的各个时点干预单据转换的相关逻辑控制. 示例: 新建一个类,继承自单据转换插件基类Kingdee.BOS.Core.Metadata.ConvertElem ...

  5. data的值 如何初始化vue_vue 创建一个基础实例【02】

    学习要点:1.框架理解    2.options    3.生命周期 本节课我们来开始学习 Vue 声明对象,并且理解传递选项对象的方法. 一.框架理解 1. 所有的前端框架基本都是为了简化:模版渲染 ...

  6. Elasticsearch:创建一个 Elasticsearch Ingest 插件

    在前面的一篇文章 "Elasticsearch:创建属于自己的 Ingest processor" 中,我相信地介绍了如何使用一个模板来创建 Ingest 插件.在今天的文章中,我 ...

  7. 多库共存 包装集的问题 点击按钮创建一个p 几个属性介绍 jQuery的插件

    多库共存 <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8 ...

  8. 50个jQuery插件可将你的网站带到另一个高度

    Web领域一直在发生变化并且其边界在过去的每一天都在发生变化(甚至不能以小时为计),随着其边界的扩展取得了许多新发展.在这些进步之中,开发者的不断工作创造了更大和更好的脚本,这些脚本以插件方式带来更好 ...

  9. 如何为您的插件创建一个很棒的WordPress.org页面

    You've just developed an awesome plugin for WordPress and you want to share it with as many people a ...

最新文章

  1. 键盘输入_键盘输入技巧
  2. SLua 中继承 C# 类接口 Slua.Class 的一个 Bug。
  3. twitter api 无法连接_光大银行牟健君:金融API的安全问题和应对技术
  4. React开发(161):onref绑定
  5. python小练习—名片管理系统(增、删、改、查、数据本地保存)
  6. java版本不兼容_java 中jdk版本不兼容的问题小记
  7. XJTUOJ wmq的队伍(树状数组求 K 元逆序对)
  8. 刷掉985/211,年底我却收到字节50W测试开发offer,实名揭露用人标准
  9. log4j+flume+HDFS实现日志存储
  10. Java实现图片上传
  11. Latex入门简明教程
  12. RINEX3文件中的toc,toe,IODE
  13. 那些年,我们一起玩过的儿童游戏
  14. 几个国内的 apple 相关社区
  15. 经典电影list(辛辛苦苦整理的)
  16. C#和JQ判断移动端还是PC端
  17. Redis RU101课程 Introduction to Redis Data Structures 第5周学习笔记
  18. mysql中删除数据库语句
  19. 2020汽车消费把脉:如何挠到年轻消费者的内心?
  20. 搭建spring config的坑 记录一下

热门文章

  1. ubuntu 修改时区、时间、同步网络时间、将时间写入硬件
  2. 2022-2028年中国企业核心路由交换机行业市场前瞻与投资分析报告
  3. 2022-2028年中国橡胶密封制品行业市场研究及前瞻分析报告
  4. c语言实现 十进制到二进制的转换
  5. ionic4中使用Swiper触屏滑动--技术
  6. 条件随机场(CRF) - 2 - 定义和形式
  7. Docker Context基本原理
  8. 在 react 里使用 antd
  9. Android开发遇到eclipse运行程序时报timeout
  10. 【spring源码分析】IOC容器初始化(六)