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. 代码静态检查Eclipse插件:SonarLint插件离线安装

    Eclipse Version: Oxygen.3a Release (4.7.3a)Myeclipse版本: 10.7 SonarLint 插件离线安装包:org.sonarlint.eclipse ...

  2. 数据仓库基础(十)Informatica 组件1

    本文转载自:http://www.cnblogs.com/evencao/p/informatica.html Informatica主要的组件: Source Qualifier 从数据源读取数据 ...

  3. linux常用命令:rpm 命令

    rpm是一个功能十分强大的软件包管理系统. 1.命令格式: rpm  [参数]  [包名] 2.命令功能: 使得在Linux下安装.升级和删除软件包的工作变得容易,并且具有查询.验证软件包的功能.与图 ...

  4. ThinkPHP内置日志记录

    ThinkPHP内置日志记录日志记录http://document.thinkphp.cn/manual_3_2.html#log 日志的处理工作是由系统自动进行的,在开启日志记录的情况下,会记录下允 ...

  5. (七)git分支的操作

    1.git branch——显示分支一览表 2.git checkout -b——创建.切换分支 往feature-A中不断add.commit叫培育分支 git checkout - 切回上一个分支 ...

  6. Django框架介绍之一

    这片博文就是对django有个大概的了解,通俗的说,就是先让django跑起来. django安装 在linux上安装如下: 源码安装: tar -zxvf Django-1.9.13.tar.gz ...

  7. 根据wsdl,apache cxf的wsdl2java工具生成客户端、服务端代码

    根据wsdl,apache cxf的wsdl2java工具生成客户端.服务端代码 apache cxf的wsdl2java工具的简单使用: 使用步骤如下: 一.下载apache cxf的包,如apac ...

  8. Android 实践项目开发二

    在地图开发中项目中,我这周主要完成的任务是和遇到的问题是以下几个方面. 1.在本次的项目中主要是利用百度地图的.jar包实现地图的定位与搜索功能,需要在百度地图开发中心网站取得 密钥,并下载相关.ja ...

  9. Bof基础实践

    Bof基础 Bof原理 Linux下进程地址空间的布局 典型的堆栈结构 上图中可以看到栈中有return address还有局部变量,也就是函数的参数,bof攻击是利用上参数的溢出将返回地址retur ...

  10. 使用fragment添加底部导航栏

    切记:fragment一定要放在framlayout中,不然不会被替换完全(就是切换之后原来的fagment可能还会存在) main.xml <LinearLayout xmlns:androi ...