扩展方式实现
本文只举两个扩展方式的例子,实现select和exists两个接口按照ID检索功能,其它类似接口,可以仿照着写:
/** * @author {CaoGuangHui} */$.extend($.fn.tabs.methods, { getTabById: function(jq,id) { var tabs = $.data(jq[0], 'tabs').tabs; for(var i=0; idiv.tabs-header>div.tabs-wrap'); var left = tab.position().left; var right = left + tab.outerWidth(); if (left < 0 || right > wrap.width()){ var deltaX = left - (wrap.width()-tab.width()) / 2; $(this).tabs('scrollBy', deltaX); } else { $(this).tabs('scrollBy', 0); } $(this).tabs('resize'); opts.onSelect.call(this, title, $(this).tabs('getTabIndex',panel)); }); }, existsById:function(jq,id){ return $(jq[0]).tabs('getTabById',id) != null; }});
使用方法:
var tab = $("tabs").tabs("selectById","tabId");var isExist = $("tabs").tabs("existsById","tabId");
这段代码扩展了三个接口,但是只有selectById和existById才会直接用到,使用这两个方法的时候传入ID参数即可。
演示实例:
引入各大需要的js
编写html
selectTab
编写js:
编写判断代码:
function selectTab(){ var isExist = $("#tt").tabs("existsById","id2"); if (isExist) { var tab = $("#tt").tabs("selectById","id2"); }else{ addPanel(); } }