手把手教你開發jquery外掛(二)
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; });
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/506/viewspace-2800836/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 手把手教你開發jquery外掛(三)jQuery
- 手把手教你開發 MyBatis 分頁外掛MyBatis
- 手把手教你開發微信小程式中的外掛微信小程式
- 手把手教你寫一個 VSCode 外掛VSCode
- 深入理解jQuery外掛開發總結(三)jQuery
- 深入理解jQuery外掛開發總結(一)jQuery
- 掌握jQuery外掛開發,這篇文章就夠了jQuery
- Vite 實戰:手把手教你寫一個 Vite 外掛Vite
- JQuery模板外掛-jquery.tmpljQuery
- VS Code外掛開發介紹(二)
- [外掛擴充套件]jQuery二維碼外掛0.2【更新瀏覽器相容】套件jQuery瀏覽器
- JQuery蜂巢圖外掛jQuery
- jQuery的外掛列表jQuery
- 手把手教你如何簡單使用laravel Modules寫外掛(1)Laravel
- VSCode外掛開發全攻略(二)HelloWordVSCode
- Grafana的Datasource外掛開發實踐二Grafana
- aspnetcore外掛開發dll熱載入 二NetCore
- 實用!開發者的 Vim 外掛(二)
- 從頭開發一個Flutter外掛(二)高德地圖定位外掛Flutter地圖
- jQuery外掛擴充套件jQuery套件
- jQuery擴充套件外掛jQuery套件
- AndroidStudio外掛GsonFormat解析及二次開發AndroidORM
- IDEA Web渲染外掛開發(二)— 自定義JsDialogIdeaWebJS
- 一篇文章,教你學會Vue CLI 外掛開發Vue
- 開發Rhino外掛
- Flutter外掛開發Flutter
- Mybatis外掛開發MyBatis
- chrome 外掛開發Chrome
- flutter 外掛開發Flutter
- VscodeIDEA開發外掛VSCodeIdea
- Webstorm 外掛開發WebORM
- Skywalking 外掛開發
- 二、外掛
- [Jenkins 外掛開發] Jenkins 外掛二次開發 - 設計一個程式碼 diff 的小工具Jenkins
- 手把手教你“開發GTA6”
- 手把手帶你開發一款提效工具--VScode外掛VSCode
- 下拉控制元件jQuery外掛控制元件jQuery
- jQuery外掛--表格隔行變色jQuery