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. POJ 分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  2. 线程等待——CountDownLatch使用

    告警性能优化过程中,遇到如下问题:1. 在数据库计算几十万个实体的KPI值的方差:2. 计算结果进行表格化处理. 这里KPI包含多个Counter的数据库函数运算(比如Decode,AVG等函数),方 ...

  3. linux常用命令:df 命令

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...

  4. 发布QT exe

    https://blog.csdn.net/u014453443/article/details/85837138

  5. 升级到php7相关问题,日请求过亿QQ会员活动平台PHP7升级实践

    升级到php7相关问题,日请求过亿QQ会员活动平台PHP7升级实践 日请求过亿:QQ会员活动平台PHP7升级实践http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4 ...

  6. java多线程----ReentrantReadWriteLock

    package com.test; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks ...

  7. CSS3实现8种Loading效果【二】

    CSS3实现8种Loading效果[二]   今晚吃完饭回宿舍又捣鼓了另外几种Loading效果,老规矩,直接“上菜“…… 注:gif图片动画有些卡顿,非实际效果! 第一种效果: 代码如下: < ...

  8. js实现ajax请求

    练下手,好久没写ajax var xmlhttp=null;//创建XMLHttprequest function createXMLHttpRequest(){ if(window.ActiveXO ...

  9. 51NOD 1069 Nim游戏

    1069 Nim游戏   有N堆石子.A B两个人轮流拿,A先拿.每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出 ...

  10. 论文笔记——SQUEEZENET ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE

    论文地址:https://arxiv.org/abs/1602.07360 模型地址:https://github.com/DeepScale/SqueezeNet 1. 论文思想 提出一种新的卷积组 ...