First, i want to add options to Tabs constructor like this:

  var tabs = $("div.tabs").tabs({

  "openEvent": "mouseover",

  "disabled": [1, 2],

  "current": 3

  });

  These options are borrowed from jQuery UI

  Tabs:

  openEvent:(String,"click")

  The type of event to be used for selecting a tab.

  disabled:(Array,[])

  An array containing the position of the tabs (zero-based index) that should be disabled on initialization.

  current:(Number,0)

  Zero-based index of the tab to be selected on initialization. To set all tabs to unselected pass -1 as value.

  The plugin code:

  (function($) {

  function Tabs(tabs, panes, options) {

  var that = this;

  this.options = {

  "openEvent": "mouseover",

  "disabled": [],

  "current": 0

  };

  $.extend(this.options, options);

  this.tabs = tabs.removeClass("current");

  this.panes = panes.hide();

  this.current = this.options.current;

  this.openTab(this.current);

  this.tabs[this.options.openEvent](function() {

  that.openTab(that.tabs.index(this));

  });

  }

  Tabs.prototype = {

  openTab: function(index) {

  this.current = index;

  if (this.current !== -1 && $.inArray(this.current, this.options.disabled) === -1) {

  this.tabs.removeClass("current").eq(this.current).addClass("current");

  this.panes.hide().eq(this.current).show();

  }

  }

  };

  $.fn.tabs = function(options) {

  var tabs = this.children("ul").find("li > a");

  var panes = this.children("div");

  return new Tabs(tabs, panes, options);

  };

  });

  Second, add some events to the plugin like this:

  var tabs = $("div.tabs").tabs({

  "openEvent": "mouseover",

  "disabled": [1, 2],

  "current": 3,

  "events": {

  "open": function(event, index) {

  console.log("[events-open]You click tab " + index);

  }

  }

  });

  The plugin source code:

  (function($) {

  function Tabs(tabs, panes, options) {

  var that = this;

  this.options = {

  "openEvent": "mouseover",

  "disabled": [],

  "current": 0,

  "events": {}

  };

  $.extend(this.options, options);

  this.tabs = tabs.removeClass("current");

  this.panes = panes.hide();

  this.current = this.options.current;

  $.each(this.options.events, function(key, value) {

  $(that).bind(key, value);

  });

  // Open current tab

  this.openTab(this.current);

  // Register open tab event

  this.tabs[this.options.openEvent](function() {

  that.openTab(that.tabs.index(this));

  });

  }

  Tabs.prototype = {

  openTab: function(index) {

  this.current = index;

  if (this.current !== -1 && $.inArray(this.current, this.options.disabled) === -1) {

  this.tabs.removeClass("current").eq(this.current).addClass("current");

  this.panes.hide().eq(this.current).show();

  $(this).trigger("open", [this.current]);

  }

  }

  };

  $.fn.tabs = function(options) {

  var tabs = this.children("ul").find("li > a");

  var panes = this.children("div");

  return new Tabs(tabs, panes, options);

  };

  });

  The result:

  [events-open]You click tab 3

  [events-open]You click tab 4

  [events-open]You click tab 0

  Notice: In this section, we bind event to a JavaScript object not the jQuery object,

  which i have mentioned in my last article.

  Third, add some methods so that we can invoke like this:

  tabs.bind("open", function(event, index) {

  console.log("[bind-open]You click tab " + index);

  });

  Source code:

  Tabs.prototype = {

  openTab: function(index) {

  // ...

  },

  bind: function(name, fn) {

  $(this).bind(name, fn);

  }

  };

  The result:

  [events-open]You click tab 3

  [events-open]You click tab 4

  [bind-open]You click tab 4

  [events-open]You click tab 3

  [bind-open]You click tab 3

  [events-open]You click tab 0

  [bind-open]You click tab 0

  Well, this series of tutorials has been finished. Pretty simple, isn’t it?(来源:程序员)

手把手教你开发jquery插件(三)的更多相关文章

  1. 手把手教你开发jquery插件

    I have said that i dislike jQuery UI’s unified API, so i want to get the instance of the component a ...

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

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

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

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

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

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

  5. 教你开发jQuery插件

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

  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. 开发JQuery插件(转)

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

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

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

随机推荐

  1. 利用<object>和<embed>实现视频播放

    直接使用<object>或<embed>都可以实现视频播放,那么两者的区别是什么? 1.是为了兼容不同浏览器,IE只支持对Object的解析:火狐,谷歌,Safari只支持对E ...

  2. PTA 团体程序设计天梯赛 L3-020 至多删三个字符

    $f[i][j]$表示到第$i$个字符,已经删去了$j$个字符的方案数. 显然的转移: $f[i][j] = f[i - 1][j] + f[i - 1][j - 1]$ 但是这样会有重复,我们考虑什 ...

  3. JsonPath的使用

    语法: JsonPath 描述 $ 根节点 @ 当前节点 .or[] 子节点 .. 选择所有符合条件的节点 * 所有节点 [] 迭代器标示,如数组下标 [,] 支持迭代器中做多选 [start:end ...

  4. python 简单的爬虫

    import urllib.request import re import ssl # 处理https请求 import time import os # 创建目录用 def get_html(ur ...

  5. ORA-00980: 同义词转换不再有效

    客户账号TB在操作软件时,报错:“[Microsoft][ODBC driver for Oracle][Oracle]ORA-00980: 同义词转换不再有效”. 使用拥有dba权限的账号sys的登 ...

  6. MySQL笔记(六)游标练习

    23.3.1 Trigger Syntax and Examples 意义不明的几道练习,留着备用. 感觉不好写,而且难以调试..不知道以后会不会有实际的应用场景. 环境:MySQL 笔记(三)由 t ...

  7. xml声明中的standalone属性

    晚上,在测试tinyxml的时候,发现其中声明了<?xml version="1.0" standalone="no" ?>,经查,其含义为stan ...

  8. inotify工具安装配置

    一.安装 1)  从内核和目录里面查看是否支持inotify [root@nfs01 ~]# uname -r 2.6.32-573.el6.x86_64 [root@nfs01 ~]# ls -l ...

  9. Java DecimalFormat 用法(数字格式化)

    我们经常要将数字进行格式化,比如取2位小数,这是最常见的.Java 提供 DecimalFormat 类,帮你用最快的速度将数字格式化为你需要的样子.下面是常用的例子: import java.tex ...

  10. Git 基础 —— 安装 配置 别名 对象

    Git 基础学习系列 Git 基础 -- 安装 配置 别名 对象 Git 基础 -- 常用命令 Git 基础 -- 常见使用场景 Git基础 -- Github 的使用 Git 安装 Git下载地址 ...