手把手教你开发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扩展三:关于本地存储数据 上一节我们 ...
随机推荐
- python recv()是什么
socket有个recv方法,recv有一个参数,指定数据缓冲区的大小 但是现在的问题就是不知道将要接受的数据的大小到底是多少,可能只有几个字节,可能会有几M,google了一下socket的入门文章 ...
- sql server数据库备份单个表的结构和数据生成脚本
1.使用场景:sql server数据库备份单个表的结构和数据,在我们要修改正式系统的数据的一天或者多条某些数据时候,要执行update语句操作,安全稳健考虑,最好先做好所修改的表的结构和数据备份! ...
- python pip list 命令列出所有安装包和版本信息
c:\Python27\Scripts>pip listDEPRECATION: The default format will switch to columns in the future. ...
- Java内存管理(一):深入Java内存区域
本文转自:http://www.cnblogs.com/gw811/archive/2012/10/18/2730117.html#undefined 推荐查看原文,原文格式更好一些. 本文引用自:深 ...
- JS函数调用的四种方法
js的函数调用会免费奉送两个而外的参数就是 this 和 arguments .arguments是参数组,他并不是一个真实的数组,但是可以使用.length方法获得长度. 书上有说4中调用方式: 方 ...
- 解决secureCRT 数据库里没有找到防火墙 '无' 此会话降尝试不通过防火墙进行连接。
解决secureCRT 数据库里没有找到防火墙 '无' 此会话降尝试不通过防火墙进行连接.的方法 中文版的CRT由于汉化的问题(把null翻译成无了),导致每次打开都会有个防火墙的错误提示:数据库里没 ...
- ACM题目————STL练习之求次数
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1112 描述 题意很简单,给一个数n 以及一个字符串str,区间[i,i+n-1] 为一个 ...
- 如何在 Linux 中将文件编码转换为 UTF-8
一个文件:vim 文件名.txt输入 :e ++enc=gbk 强制用gbk打开输入 :w ++enc=utf8 转换到utf8保存. 多个文件:for i in *.txt; do iconv -f ...
- Poisson Blending(Seamless clone)研究和实现
Poisson Blending 实现了非常棒的效果,可以看 <自己动手,实现“你的名字”滤镜> http://www.cnblogs.com/jsxyhelu/p/7216795.htm ...
- 20145101《Java程序设计》第三周学习总结
20145101 <Java程序设计>第3周学习总结 教材学习内容总结 本周进行的是第四章和第五章的学习.本阶段的学习难度有所提升,无论是在知识的量还是深度都开始增加,内容很丰富,也很有趣 ...