自己动手开发jQuery插件全面解析 jquery插件开发方法
- jQuery.foo = function() {
- alert('This is a test. This is only a test.');
- };
- jQuery.foo = function() {
- alert('This is a test. This is only a test.');
- };
- jQuery.bar = function(param) {
- alert('This function takes a parameter, which is "' + param + '".');
- };
- 调用时和一个函数的一样的:jQuery.foo();jQuery.bar();或者$.foo();$.bar('bar');
- jQuery.extend({
- foo: function() {
- alert('This is a test. This is only a test.');
- },
- bar: function(param) {
- alert('This function takes a parameter, which is "' + param +'".');
- }
- });
- jQuery.myPlugin = {
- foo:function() {
- alert('This is a test. This is only a test.');
- },
- bar:function(param) {
- alert('This function takes a parameter, which is "' + param + '".');
- }
- };
- 采用命名空间的函数仍然是全局函数,调用时采用的方法:
- $.myPlugin.foo();
- $.myPlugin.bar('baz');
- (function($){
- $.fn.extend({
- pluginName:function(opt,callback){ //这个callback还没有理解
- // Our plugin implementation code goes here.
- }
- })
- })(jQuery);
形式2:
- (function($) {
- $.fn.pluginName = function() {
- // Our plugin implementation code goes here.
- };
- })(jQuery);
上面定义了一个jQuery函数,形参是$,函数定义完成之后,把jQuery这个实参传递进去.立即调用执行。这样的好处是,我们在写jQuery插件时,也可以使用$这个别名,而不会与prototype引起冲突.
- $.fn.hilight = function() {
- // Our plugin implementation code goes here.
- };
- 我们的插件通过这样被调用:
- $('#myDiv').hilight();
2.2 接受options参数以控制插件的行为
- // plugin definition
- $.fn.hilight = function(options) {
- var defaults = {
- foreground: 'red',
- background: 'yellow'
- };
- // Extend our default options with those provided.
- var opts = $.extend(defaults, options); //这里是对defaults对象进行扩展
- // Our plugin implementation code goes here.
- };
- 我们的插件可以这样被调用:
- $('#myDiv').hilight({
- foreground: 'blue'
- });
- // plugin definition
- $.fn.hilight = function(options) {
- // Extend our default options with those provided.
- // Note that the first arg to extend is an empty object -
- // this is to keep from overriding our "defaults" object.
- var opts = $.extend({}, $.fn.hilight.defaults, options); //这是扩展$.fn.hilight.defaults名空间下进行扩展,这样就可以满足客户端的链式调用
- // Our plugin implementation code goes here.
- };
- // plugin defaults - added as a property on our plugin function
- $.fn.hilight.defaults = { //在hilight的名空间下,有添加了一个名空间
- foreground: 'red',
- background: 'yellow'
- };
- 现在使用者可以包含像这样的一行在他们的脚本里:
- //这个只需要调用一次,且不一定要在ready块中调用
- $.fn.hilight.defaults.foreground = 'blue';
- 接下来我们可以像这样使用插件的方法,结果它设置蓝色的前景色:
- $('#myDiv').hilight();
- // plugin definition
- $.fn.hilight = function(options) {
- // iterate and reformat each matched element
- return this.each(function() {
- var $this = $(this); //最好写成这样,因为$this,一看就知道是jquery对象了,不会让人以为是原始对象
- // ...
- var markup = $this.html();
- // call our format function
- markup = $.fn.hilight.format(markup);
- $this.html(markup);
- });
- };
- // define our format function
- $.fn.hilight.format = function(txt) {
- return '<strong>' + txt + '</strong>';
- };
我们很容易的支持options对象中的其他的属性通过允许一个回调函数来覆盖默认的设置。这是另外一个出色的方法来修改你的插件。这里展示的技巧是进一步有效的暴露format函数进而让他能被重新定义。通过这技巧,是其他人能够传递他们自己设置来覆盖你的插件,换句话说,这样其他人也能够为你的插件写插件。
考虑到这个篇文章中我们建立的无用的插件,你也许想知道究竟什么时候这些会有用。一个真实的例子是Cycle插件.这个Cycle插件是一个滑动显示插件,他能支持许多内部变换作用到滚动,滑动,渐变消失等。但是实际上,没有办法定义也许会应用到滑动变化上每种类型的效果。那是这种扩展性有用的地方。 Cycle插件对使用者暴露"transitions"对象,使他们添加自己变换定义。插件中定义就像这样:
- (function($) {
- // plugin definition
- $.fn.hilight = function(options) {
- debug(this);
- // ...
- };
- // private function for debugging
- function debug($obj) {
- if (window.console && window.console.log)
- window.console.log('hilight selection count: ' + $obj.size());
- };
- // ...
- })(jQuery);
- $.fn.hilight = function(options) {
- // ...
- // build main options before element iteration
- var opts = $.extend({}, $.fn.hilight.defaults, options);
- return this.each(function() {
- var $this = $(this);
- // build element specific options
- var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
- //...
- <!-- markup -->
- <div class="hilight { background: 'red', foreground: 'white' }">
- Have a nice day!
- </div>
- <div class="hilight { foreground: 'orange' }">
- Have a nice day!
- </div>
- <div class="hilight { background: 'green' }">
- Have a nice day!
- </div>
- 现在我们能高亮哪些div仅使用一行脚本:
- $('.hilight').hilight();
下面使我们的例子完成后的代码:
- // 创建一个闭包
- (function($) {
- // 插件的定义
- $.fn.hilight = function(options) {
- debug(this);
- // build main options before element iteration
- var opts = $.extend({}, $.fn.hilight.defaults, options);
- // iterate and reformat each matched element
- return this.each(function() {
- $this = $(this);
- // build element specific options
- var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
- // update element styles
- $this.css({
- backgroundColor: o.background,
- color: o.foreground
- });
- var markup = $this.html();
- // call our format function
- markup = $.fn.hilight.format(markup);
- $this.html(markup);
- });
- };
- // 私有函数:debugging
- function debug($obj) {
- if (window.console && window.console.log)
- window.console.log('hilight selection count: ' + $obj.size());
- };
- // 定义暴露format函数
- $.fn.hilight.format = function(txt) {
- return '<strong>' + txt + '</strong>';
- };
- // 插件的defaults
- $.fn.hilight.defaults = {
- foreground: 'red',
- background: 'yellow'
- };
- // 闭包结束
- })(jQuery);
jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。
自己动手开发jQuery插件全面解析 jquery插件开发方法的更多相关文章
- 转 jquery插件--241个jquery插件—jquery插件大全
241个jquery插件—jquery插件大全 jquery插件jqueryautocompleteajaxjavascriptcoldfusion jQuery由美国人John Resig创建,至今 ...
- 【jQuery插件】用jQuery Masonry快速构建一个pinterest网站布局(转)
[jQuery插件]用jQuery Masonry快速构建一个pinterest网站布局 时间:2011年03月21日作者:愚人码头查看次数:29,744 views评论次数:25条评论 前段时间领导 ...
- JQUERY插件学习之jQuery UI
jQuery UI:http://jqueryui.com/ jQuery UI介绍: jQuery UI 是以 jQuery 为基础的开源 JavaScript 网页用户界面代码库.包含底层用户交互 ...
- jQuery 源码解析(三) pushStack方法 详解
该函数用于创建一个新的jQuery对象,然后将一个DOM元素集合加入到jQuery栈中,最后返回该jQuery对象,有三个参数,如下: elems Array类型 将要压入 jQuery 栈的数组元素 ...
- jQuery插件:图片放大镜--jQuery Zoom
本文转载于http://blog.csdn.net/xinhaozheng/article/details/4085644, 这是一款非常不错的给图片添加放大镜效果,可以应用在诸如zen cart,m ...
- 多日期选择jQuery插件 MultiDatesPicker for jQuery UI
Multiple-Dates-Picker-for-jQuery-UI是一个多日期选择的jquery控件. GIT源码: https://github.com/dubrox/Multiple-Da ...
- jQuery插件实例六:jQuery 前端分页
先来看看效果: 对于前端分页,关键是思路,和分页算法.本想多说两句,可又觉得没什么可说的,看代码吧: 如何使用? $("#pging").zPagination({ 'navEve ...
- 十七.jQuery源码解析之入口方法Sizzle(1)
函数Sizzle(selector,context,results,seed)用于查找与选择器表达式selector匹配的元素集合.该函数是选择器引擎的入口. 函数Sizzle执行的6个关键步骤如下: ...
- 使用 Eclipse 可视化插件 windowbuilder 进行Java GUI开发(插件安装的两种方法)
对于Java GUI开发 其实最方便的方法是用插件制作,当然先了解完代码原理是最好的. eclispe安装windowbuilder有两种方式,一种是离线安装,一种是在线安装. 一.第一种在线安装: ...
随机推荐
- js数组中foEach和map的用法详解 jq中的$.each和$.map
数组中foEach和map的用法详解 相同点: 1.都是循环遍历数组(仅仅是数组)中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项value, ...
- Swift学习笔记(9)--闭包
1.闭包表达式: { (parameters) -> returnType in statements } 注1.闭包表达式语法可以使用常量.变量和inout类型作为参数,不提供默认值. 也可以 ...
- Swift学习笔记(8)--函数
1.定义及调用 func sayHelloAgain(personName: String) -> String { return "Hello again, " + per ...
- 洛谷 P1889 士兵站队
P1889 士兵站队 题目描述 在一个划分成网格的操场上, n个士兵散乱地站在网格点上.由整数 坐标 (x,y) 表示.士兵们可以沿网格边上.下左右移动一步,但在同时刻任一网格点上只能有名士兵.按照军 ...
- POJ 1014 Dividing 背包
二进制优化,事实上是物体的分解问题. 就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1. 2, 4, 6 假设这个物体有数量为25,那么就分解为1, 2, 4. 8. 10 看出规 ...
- qwt的安装和移植-
目须要依据实时数据绘制出图表,因此我们找到了qwt库.这个库是一个绘制图表,曲线图,柱状图的统计图标. . .. 以下我们就具体解说一下这个库在Larm上的编译和移植 qwt介绍 QWT,全称是Qt ...
- PipeCAD之管道标准库PipeStd
PipeCAD之管道标准库PipeStd eryar@163.com Key Words. PipeCAD, PipeStd, 管道设计软件,管件库 1. Introduction 前不久,两位老友徐 ...
- CentOS 7.0yum安装MySQL
CentOS 7.0yum安装MySQL 1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noar ...
- Codefroces Educational Round 27 845G Shortest Path Problem?
Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...
- maven项目引入sqljdbc4 找不到包的完美 解决方案
今天碰到了这个问题,解决了,顺便做一下记录.首先来 重现 一下这个问题,maven install报错,说 找不到这个包,但是其实 我已经安装了. 我们 再来 看看 maven本地仓库里面有 什么,这 ...