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. Intro to Python for Data Science Learning 2 - List

    List from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-2-python-list ...

  2. C++矩阵库 Eigen 快速入门

    最近需要用 C++ 做一些数值计算,之前一直采用Matlab 混合编程的方式处理矩阵运算,非常麻烦,直到发现了 Eigen 库,简直相见恨晚,好用哭了. Eigen 是一个基于C++模板的线性代数库, ...

  3. SLF4J和log4j的使用

    概念 SLF4J:即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统.按照官方的说法,SLF4J是一个用于日志系统的简 ...

  4. 浅谈为什么一个java源文件中只能有一个public类?

    声明,本篇文章为转载 转载 http://blog.csdn.net/bareheadzzq/article/details/6562211 最近在一个java文件中实现了几个类,其中一个声明为pub ...

  5. Linux服务器配置---安装telnet

    安装telnet      telnet是标准的远程登录协议,历史悠久.但是telnet的对话数据没有加密,甚至用户名和密码都是明文显示,这样的服务风险极大.目前大多数系统多已经不会再安装这个服务了, ...

  6. mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)字符串拼接cancat实战例子

    mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)mysql字符串拼接cancat实战例子: mysql update set 多个字段相加,如果是数字相加可以直接用+号(注:hund ...

  7. python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字

    python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字在字符串里面插入指定分割符的方法,先把字符串变成list然后用join方法变成字符串str=' ...

  8. 20165211 2017-2018-2 《Java程序设计》第4周学习总结

    20165211 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 本周,我学习了书本上第五.六两章的内容,以下是我整理的主要知识. 第五章 子类与继承 子类与 ...

  9. 51NOD 1099 任务执行顺序

    来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1099 前天没睡好 昨天做题闷闷沉沉的 好多一眼题 都瞎做了 这题今 ...

  10. Avito Cool Challenge 2018

    考挂了.. A - Definite Game 直接看代码吧. #include<cstdio> #include<cstring> #include<algorithm ...