手把手教你开发jquery插件(三)
First, i want to add options to Tabs constructor like this:
var tabs = $("div.tabs").tabs({
"openEvent": "mouseover",
"disabled": [1, 2],
"current": 3
});
These options are borrowed from jQuery UI
Tabs:
openEvent:(String,"click")
The type of event to be used for selecting a tab.
disabled:(Array,[])
An array containing the position of the tabs (zero-based index) that should be disabled on initialization.
current:(Number,0)
Zero-based index of the tab to be selected on initialization. To set all tabs to unselected pass -1 as value.
The plugin code:
(function($) {
function Tabs(tabs, panes, options) {
var that = this;
this.options = {
"openEvent": "mouseover",
"disabled": [],
"current": 0
};
$.extend(this.options, options);
this.tabs = tabs.removeClass("current");
this.panes = panes.hide();
this.current = this.options.current;
this.openTab(this.current);
this.tabs[this.options.openEvent](function() {
that.openTab(that.tabs.index(this));
});
}
Tabs.prototype = {
openTab: function(index) {
this.current = index;
if (this.current !== -1 && $.inArray(this.current, this.options.disabled) === -1) {
this.tabs.removeClass("current").eq(this.current).addClass("current");
this.panes.hide().eq(this.current).show();
}
}
};
$.fn.tabs = function(options) {
var tabs = this.children("ul").find("li > a");
var panes = this.children("div");
return new Tabs(tabs, panes, options);
};
});
Second, add some events to the plugin like this:
var tabs = $("div.tabs").tabs({
"openEvent": "mouseover",
"disabled": [1, 2],
"current": 3,
"events": {
"open": function(event, index) {
console.log("[events-open]You click tab " + index);
}
}
});
The plugin source code:
(function($) {
function Tabs(tabs, panes, options) {
var that = this;
this.options = {
"openEvent": "mouseover",
"disabled": [],
"current": 0,
"events": {}
};
$.extend(this.options, options);
this.tabs = tabs.removeClass("current");
this.panes = panes.hide();
this.current = this.options.current;
$.each(this.options.events, function(key, value) {
$(that).bind(key, value);
});
// Open current tab
this.openTab(this.current);
// Register open tab event
this.tabs[this.options.openEvent](function() {
that.openTab(that.tabs.index(this));
});
}
Tabs.prototype = {
openTab: function(index) {
this.current = index;
if (this.current !== -1 && $.inArray(this.current, this.options.disabled) === -1) {
this.tabs.removeClass("current").eq(this.current).addClass("current");
this.panes.hide().eq(this.current).show();
$(this).trigger("open", [this.current]);
}
}
};
$.fn.tabs = function(options) {
var tabs = this.children("ul").find("li > a");
var panes = this.children("div");
return new Tabs(tabs, panes, options);
};
});
The result:
[events-open]You click tab 3
[events-open]You click tab 4
[events-open]You click tab 0
Notice: In this section, we bind event to a JavaScript object not the jQuery object,
which i have mentioned in my last article.
Third, add some methods so that we can invoke like this:
tabs.bind("open", function(event, index) {
console.log("[bind-open]You click tab " + index);
});
Source code:
Tabs.prototype = {
openTab: function(index) {
// ...
},
bind: function(name, fn) {
$(this).bind(name, fn);
}
};
The result:
[events-open]You click tab 3
[events-open]You click tab 4
[bind-open]You click tab 4
[events-open]You click tab 3
[bind-open]You click tab 3
[events-open]You click tab 0
[bind-open]You click tab 0
Well, this series of tutorials has been finished. Pretty simple, isn’t it?(来源:程序员)
手把手教你开发jquery插件(三)的更多相关文章
- 手把手教你开发jquery插件
I have said that i dislike jQuery UI’s unified API, so i want to get the instance of the component a ...
- 教你开发jQuery插件(转)
教你开发jQuery插件(转) 阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原文: ...
- 手把手教你开发Chrome扩展三:关于本地存储数据
手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 HTML5 ...
- 【转】教你开发jQuery插件
阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原文:http://www.cnblo ...
- 教你开发jQuery插件
jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...
- 手把手教你开发chrome扩展
转载:http://www.cnblogs.com/walkingp/archive/2011/04/04/2003875.html 手把手教你开发chrome扩展一:开发Chrome Extenst ...
- 手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单
手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩 ...
- 开发JQuery插件(转)
教你开发jQuery插件(转) 阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原 ...
- 手把手教你开发Chrome扩展二:为html添加行为
手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 上一节我们 ...
随机推荐
- JVM内存模型:程序计数器
一.JVM模型概述 java虚拟机(JVM)在java程序运行的过程中,会将它所管理的内存划分为若干个不同的数据区域,这些区域有的随着JVM的启动而创建,有的随着用户线程的启动和结束而建立和销毁.一个 ...
- HTTP 协议入门
本文转载自:http://www.ruanyifeng.com/blog/2016/08/http.html HTTP 协议是互联网的基础协议,也是网页开发的必备知识,最新版本 HTTP/2 更是让它 ...
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...
- python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL
python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...
- P4009 汽车加油行驶问题
P4009 汽车加油行驶问题 最短路 清一色的spfa....送上一个堆优化Dijkstra吧(貌似代码还挺短) 顺便说一句,堆优化Dj跑分层图灰常好写 #include<iostream> ...
- P3294 [SCOI2016]背单词
P3294 [SCOI2016]背单词 Trie+贪心 倒插进树+取出重建+子树处理+贪心遍历 倒插进树:把后缀转化为前缀,所以把字符串倒着插进Trie中 取出重建:重新建立一棵以单词为节点的树,如果 ...
- HTML5 Chart.js 框架
HTML5 Chart.js 框架 版权声明:未经博主授权,内容严禁转载 ! Chart.js 概述: chart.js 是一个简单的.面向对象.为设计者开发者准备的图表绘制工具. 点击进入官方网址 ...
- 20165310java_teamExp1_week1
结对编程项目-四则运算-week1 需求分析 第一周达成 支持真分数的四则运算 支持多运算符 能手动输入n道题目,n由使用者输入 后续拓展的可能 能随机生成n道题目,n由使用者输入 能够判断正误,错误 ...
- 在Visual C#中使用XML指南之读取XML
网站:http://www.yesky.com/155/1915155all.shtml#p1915155
- Pairs Forming LCM (LCM+ 唯一分解定理)题解
Pairs Forming LCM Find the result of the following code: ; i <= n; i++ ) for( int j = i; j ...