手把手教你开发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扩展三:关于本地存储数据 上一节我们 ...
随机推荐
- Java设计模式应用——组合模式
组合模式实际上是一种树形数据结构.以windows目录系统举例,怎么样用java语言描述一个文件夹? 定义一个文件夹类,文件夹类中包含若干个子文件类和若干个文件类. 进一步抽象,把文件夹和文件都看做节 ...
- linux常用命令:ln 命令
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...
- foxmail收取163企业邮箱设置,不能直接用foxmail默认的配置,否则一直提示帐号密码错误
foxmail收取163企业邮箱设置,不能直接用foxmail默认的配置,否则一直提示帐号密码错误,收件.发件服务器配置需要用imap.ym.163.com,smtp.ym.163.com三级域名,帐 ...
- C/C++之单例模式实现
/*** * 保证一个类仅有一个实例,并提供一个访问它的全局访问点 */ #include <iostream> #include <string> using namespa ...
- python之路----进程(一)
一.理论知识1.操作系统发展简介 1.没有操作系统 —— 穿孔卡片 2.批处理系统 —— 串行 ,速度块 联机批处理 读磁带的时候速度快 脱机批处理 读磁带和cpu工作并发 3.多道程序系统 —— 并 ...
- 干货:Java并发编程系列之synchronized(一)
1. 使用方法 synchronized 是 java 中最常用的保证线程安全的方式,synchronized 的作用主要有三方面: 确保线程互斥的访问代码块,同一时刻只有一个方法可以进入到临界区 保 ...
- UVA644 Immediate Decodability
UVA644 Immediate Decodability Trie Trie模板题 难度几乎相等的题P2580 于是他错误的点名开始了 对于每组数据都清空树太浪费时间,所以我们只要在需要新点时预先把 ...
- 20145304 Exp9 Web安全基础实践
20145304 Exp9 Web安全基础实践 实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入是将查询语句当做查询内容输入到查询的框中,以此来使服务器执行攻击者想让它执行的语句,而不是 ...
- 20145330 《网络对抗》PC平台逆向破解:注入shellcode 和 Return-to-libc 攻击实验
20145330 <网络对抗>PC平台逆向破解:注入shellcode 实验步骤 1.用于获取shellcode的C语言代码 2.设置环境 Bof攻击防御技术 需要手动设置环境使注入的sh ...
- [noip模拟题]合理种植
[问题描述] 大COS在氯铯石料场干了半年,受尽了劳苦,终于决定辞职.他来到表弟小cos的寒树中学,找到方克顺校长,希望寻个活干. 于是他如愿以偿接到了一个任务…… 美丽寒树中学种有许多寒树.方克顺希 ...