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插件的更多相关文章

  1. 手把手教你开发jquery插件(三)

    First, i want to add options to Tabs constructor like this: var tabs = $("div.tabs").tabs( ...

  2. 教你开发jQuery插件(转)

    教你开发jQuery插件(转) 阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原文: ...

  3. 【转】教你开发jQuery插件

    阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原文:http://www.cnblo ...

  4. 教你开发jQuery插件

    jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...

  5. 开发JQuery插件(转)

    教你开发jQuery插件(转)   阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原 ...

  6. 手把手教你开发chrome扩展

    转载:http://www.cnblogs.com/walkingp/archive/2011/04/04/2003875.html 手把手教你开发chrome扩展一:开发Chrome Extenst ...

  7. 手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单

    手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单   手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩 ...

  8. 手把手教你开发Chrome扩展三:关于本地存储数据

    手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 HTML5 ...

  9. 手把手教你开发Chrome扩展二:为html添加行为

    手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 上一节我们 ...

随机推荐

  1. 用js或css实现淡入淡出

    淡入淡出?你问我有什么用? 提升首页13格的东西,你居然不知道!! 好啦,不废话了,正文. 1 js 主要元素:fadeIn()   fadeOut() show hide 2 css 主要元素: o ...

  2. n是否是2的幂

    实例五:n是否是2的幂 方法一:result=n&(n-1) 如果result=0 则n是2的幂方法二:result=n&((~n)+1) 如果result=n 则n是2的幂 原数   ...

  3. python3 集合的常用方法

    方法 意义 S.add(e) 在集合中添加一个新的元素e:如果元素已经存在,则不添加 S.remove(e) 从集合中删除一个元素,如果元素不存在于集合中,则会产生一个KeyError错误 S.dis ...

  4. linux常用命令:tar 命令

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...

  5. tomcat性能调优 大赞

    从“第三天”的性能测试一节中,我们得知了决定性能测试的几个重要指标,它们是: ü 吞吐量 ü Responsetime ü Cpuload ü MemoryUsage 我 们也在第三天的学习中对Apa ...

  6. selinux配置错误实例介绍

    错误原因 配置关闭SELinux,结果误操作 应修改配置文件/etc/selinux/config中的“SELINUX”参数的值, # SELINUX=enforcing  原始配置 SELINUX= ...

  7. win10锁屏界面无法更新

    win10的锁屏界面都是巨硬公司推送过来的,质量还不错,最近锁屏界面无法更新,解决方案如下: 以管理员身份运行cmd,分别运行如下两个命令 del /f /s /q /a "%userpro ...

  8. P3498 [POI2010]KOR-Beads

    P3498 [POI2010]KOR-Beads 题解 hash+hash表+调和级数 关于调和级数(from baidu百科): 调和级数发散的速度非常缓慢.举例来说,调和序列前10项的和还不足10 ...

  9. html判断当前页面是否在iframe中/顶级document中

    在使用div+iframe布局的应用中,通常我们希望在session超时或者未登录访问时跳转到登录页面,默认情况下iframe中的页面无法直接覆盖父页面,因此需要在登录页面加载的时候判断一下当前是否为 ...

  10. 03: zabbix API接口 对 主机、主机组、模板、应用集、监控项、触发器等增删改查

    目录:Django其他篇 01: 安装zabbix server 02:zabbix-agent安装配置 及 web界面管理 03: zabbix API接口 对 主机.主机组.模板.应用集.监控项. ...