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. 24最小生成树之Prim算法

    最小生成树的Prim算法 思想:采用子树延伸法 将顶点分成两类: 生长点——已经在生成树上的顶点 非生长点——未长到生成树上的顶点 使用待选边表: 每个非生长点在待选边表中有一条待选边,一端连着非生长 ...

  2. nodejs中req.body对请求参数的解析问题

    首先,先了解一下关于http协议里定义的四种常见数据的post方法,分别是: application/www-form-ulrencoded multipart/form-data applicati ...

  3. 人工智能范畴及深度学习主流框架,谷歌 TensorFlow,IBM Watson认知计算领域IntelligentBehavior介绍

    人工智能范畴及深度学习主流框架,谷歌 TensorFlow,IBM Watson认知计算领域IntelligentBehavior介绍 ================================ ...

  4. nohup 命令(设置后台进程): appending output to ‘nohup.out’ 问题

    一.Linux 下使用 nohup Unix/Linux下一般比如想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行. 比如我们要运行weblogic在后台:./startW ...

  5. MySQL数据库----存储引擎

    什么是存储引擎? 存储引擎说白了就是如何存储数据.如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法.因为在关系数据库中数据的存储是以表的形式存储的,所以存储引擎也可以称为表类型(即存储和操 ...

  6. python之路----黏包的解决方案

    黏包的解决方案 远程执行命令 # server 下发命令 给client import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) ...

  7. MySQL笔记(二)数据库对象的创建和管理

    学校用 sqlserver ,记录数据移植到 mysql 过程中的一些问题(对应数据类型,主键外键等). 索引: 查看数据的物理路径 查看表相关的信息(SHOW CREATE TABLE.DESC) ...

  8. Python3基础 os listdir 列举指定的所有文件及文件夹的名字

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. linux下使用docker-thunder-xware进行离线下载

    1.环境: lsb_release -a hello@jhello:~$ lsb_release -aNo LSB modules are available.Distributor ID: Ubun ...

  10. hdu 6180 Schedule

    Schedule Problem Description There are N schedules, the i-th schedule has start time si and end time ...