我做了这个 fiddle :
现在,我想做的是,如果选项卡将 selectedTab 作为其类,则显示相应的 div。我已经设置了 if 语句,但不确定从哪里开始,
感谢任何帮助!
最佳答案
看看this revision .
我已将显示选项卡的代码分解为函数 showTab
:
var showTab = function(id) {
$tab = $("#" + id);
$('.tabTrigger').removeClass('selectedTab');
$tab.addClass('selectedTab');
$('.tabContent').hide();
$('#' + id.replace('tab','content')).show();
}
剩下的就很简单了:
// Show the selected tab, or the first one if none is selected
var selectedId = $('.tabTrigger.selectedTab').attr('id');
showTab(selectedId || $('.tabTrigger:first').attr('id'));
// Set up the click handlers
$('.tabTrigger').click(function(){
showTab(this.id);
});
作为一般说明,在所有此类情况下,您都需要将要运行的代码分解为独立函数。一旦你做到了这一点,剩下的就自然而然了。
关于jQuery if hasClass 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7332363/