/*
* jquery.tab
* Author: 冬日小草
* Date: 2010/12/07
*/
jQuery.fn.tab = function(options) {
var settings =
{
activeTabClass: "tab-selected",
defaultTabClass: "tab-default",
tabContainerClass: "tabContainer",
tabPanelCls: "tabPanel",
mouseoverTabClass: null,
hiddenTabClass: 'tab-hide',
tabPanel: null,
selectHandler: null,
iframeIdPrex: 'iframe_'
};
if (options) {
jQuery.extend(settings, options);
}
//#region public events
$.fn.setActiveTab = function(tabIndex) {
if (tabIndex) {
return this.each(function() {
this.setActiveTab(tabIndex);
})
}
};
$.fn.getFrameByTabId = function(tabId) {
if (tabId) {
var iframeId = settings.iframeIdPrex + tabId;
return frames[iframeId];
}
return null;
};
//#endregion public events
return this.each(function() {
var ts = this;
var $tabContainer = $(ts);
ts.activeTab = null;
ts.tabPanelId = null;
ts.selectedTab = null;
ts.selectedIndex = 0;
ts.iframeId = null;
//#region 'private' methods
this.setActiveTab = function(tabIndex) {
if (typeof (tabIndex) != "number") {
return;
}
var selectedTab = $('li:visible', $tabContainer).eq(tabIndex);
if (selectedTab.length == 0) {
return;
}
//click the active tab
if (ts.iframeId == settings.iframeIdPrex + selectedTab.attr('id')) {
return;
}
else {
if (ts.iframeId != null) {
//$(frames[activeTabId]).hide();
$("iframe").hide();
}
}
$('.' + settings.activeTabClass, $tabContainer).removeClass(settings.activeTabClass);
ts.activeTab = selectedTab;
ts.activeTab.addClass(settings.activeTabClass);
var target = ts.activeTab.attr('target');
if (typeof (target) != 'string') {
return;
}
ts.iframeId = settings.iframeIdPrex + selectedTab.attr('id');
if ($('#' + ts.iframeId).length == 0) {
var iframe = $('
');
iframe.attr('id', ts.iframeId)
.attr('src', target)
.css({ width: '100%', height: '100%' });
iframe.appendTo(settings.tabPanel);
}
else {
$('#' + ts.iframeId).show();
}
};
var initialTabs = function() {
$tabContainer.addClass(settings.tabContainerClass);
$(settings.tabPanel).addClass(settings.tabPanelCls);
var stopFloatDiv = $('
');
stopFloatDiv.css({ clear: 'both', height: '0px' })
.insertAfter($tabContainer);
$('li', $tabContainer).each(function(i) {
var $tab = $(this);
var $link = $('a', $tab);
var href = $link.attr('href');
$link.attr('href', "#");
$tab.attr('target', href)
.addClass(settings.defaultTabClass)
.click(function(e) {
ts.selectedTab = $tab;
ts.selectedIndex = i;
if (typeof (settings.selectHandler) == "function") {
settings.selectHandler();
}
else {
ts.setActiveTab(i);
}
})
});
};
//#endregion 'private' methods
initialTabs();
ts.setActiveTab(0); //set first tab active as default.
});
};
1. 此tab用了三个dom元素