手把手教你开发jquery插件
I have said that i dislike jQuery UI’s unified API, so i want to
get the instance of the component after invoke like this:
$(function() {
var tabs = $("div.tabs").tabs();
// Note: Now tabs is the instance of the component
window.setTimeout(function() {
tabs.clickTab(2);
}, 2000);
});
To achieve this, i modified the plugin code:
(function($) {
function Tabs(tabs, panes) {
var that = this;
this.tabs = tabs;
this.panes = panes;
this.current = 0;
this.clickTab(0);
this.tabs.click(function() {
that.clickTab(that.tabs.index(this));
});
}
Tabs.prototype = {
clickTab: function(index) {
this.current = index;
this.tabs.removeClass("current").eq(this.current).addClass("current");
this.panes.hide().eq(this.current).show();
}
};
$.fn.tabs = function() {
var tabs = this.children("ul").find("li > a");
var panes = this.children("div");
return new Tabs(tabs, panes);
};
})
Note that Tabs is defined in the self-execution function, so it will be hidden
from the outside world.
And we public the clickTab metod in the prototype. I works well.
This article is over, below is some advance topics.
====================================================
How to extend Tabs class?
It maybe a little difficult because it’s a private function.
Never mind, just change the prototype of $.fn.tabs:
(function($) {
function Tabs(tabs, panes) {
// ...
}
Tabs.prototype = {
// ...
};
$.fn.tabs = function() {
// ...
};
$.fn.tabs.prototype = Tabs.prototype;
});
We can extend the Tabs class like this:
$.fn.tabs.prototype.getLength = function() {
return this.tabs.length;
};
$(function() {
var tabs = $("div.tabs").tabs();
alert(tabs.getLength());
});
Or we can use the method introduced in jQuery core, which is described in
my last post.
(function($) {
$.fn.tabs = function() {
var tabs = this.children("ul").find("li > a");
var panes = this.children("div");
return new $.fn.tabs.prototype.init(tabs, panes);
};
$.fn.tabs.prototype = {
init: function(tabs, panes) {
var that = this;
this.tabs = tabs;
this.panes = panes;
this.current = 0;
this.clickTab(0);
this.tabs.click(function() {
that.clickTab(that.tabs.index(this));
});
},
clickTab: function(index) {
this.current = index;
this.tabs.removeClass("current").eq(this.current).addClass("current");
this.panes.hide().eq(this.current).show();
}
};
$.fn.tabs.prototype.init.prototype = $.fn.tabs.prototype;
});
手把手教你开发jquery插件的更多相关文章
- 手把手教你开发jquery插件(三)
First, i want to add options to Tabs constructor like this: var tabs = $("div.tabs").tabs( ...
- 教你开发jQuery插件(转)
教你开发jQuery插件(转) 阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原文: ...
- 【转】教你开发jQuery插件
阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原文:http://www.cnblo ...
- 教你开发jQuery插件
jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...
- 开发JQuery插件(转)
教你开发jQuery插件(转) 阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原 ...
- 手把手教你开发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扩 ...
- 手把手教你开发Chrome扩展三:关于本地存储数据
手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 HTML5 ...
- 手把手教你开发Chrome扩展二:为html添加行为
手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 上一节我们 ...
随机推荐
- QQ 客服设置
不说那么多了. 目前可以通过此方式实现添加的效果 <a target="_blank" href="http://wpa.qq.com/msgrd?v=3& ...
- javascript技巧及常用事件方法集合(全)
事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event.srcE ...
- 简化document.createElement("div")动态生成层方法
我们在WEB开发时,很多时候往往需要我们 JavaScript 来动态建立 html 元素,动态的设置相关的属性.比方说我们想要建立一個 div 层,则可以使用以下代码实现. 一.直接建立functi ...
- 文件和打印机共享 win7 and xp
Win7 摘自:https://www.xp510.com/article/4249.html 首先开启服务 方法:开始---所有程序---附件---运行---输入services.msc----确定 ...
- 组合类C++
C++中类的组合 ※组合的概念 ×类中的成员是另一个类的对象. ×可以在已有的抽象的基础上实现更加复杂的抽象. 通过对复杂对象进行分解.抽象,使我们能够将一个复杂对象 理解为简单对象的组合. 分解得到 ...
- C++面向对象高级开发课程(第三周)
一,类与类之间的关系:继承(Inheritance).复合(Composition).委托(Delegation). 二,复合:表示 is-a ,该设计思想可以参照C语言的 struct . 1. 例 ...
- 2018-2019-1 20189218《Linux内核原理与分析》第八周作业
编译链接的过程 编译就是把文本形式源代码翻译为机器语言形式的目标文件过程. 链接是把目标文件.操作系统的启动代码和用到的库文件进行组织最终形成可执行代码的过程. 对于GCC来说,编译源代码并最终形成可 ...
- VC++实现程序重启的方法(转载)
转载:http://blog.csdn.net/clever101/article/details/9327597 很多时候系统有很多配置项,修改了配置项之后能有一个按钮实现系统重启.所谓重启就是杀死 ...
- BZOJ 2141 排队(树状数组套treap)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2141 题意:给出一个数列A,每次交换两个数的位置.输出交换后逆序对的个数. 思路:首先, ...
- JavaScript:正则表达式 分组
在现在的我看来,带小挂号的就是分组,嗯. 代码: var reg=/(abc)/; var str="abcdabcdeabcdef"; console.dir(reg.exec( ...