【发布时间】:2012-02-12 06:51:22
【问题描述】:
我正在使用此代码进行简单的 jQuery 选项卡设置:
$('.tabs .tab_content').hide(); // Hide all divs
$('.tabs .tab_content:first').show(); // Show the first div
$('.tabs ul.tab_nav li:first').addClass('current_tab'); // Set the class of the first link to active
$('.tabs ul.tab_nav li a').click(function(){ //When any link is clicked
$('.tabs ul.tab_nav li a').removeClass('current_tab'); // Remove active class from all links
$(this).addClass('current_tab'); //Set clicked link class to active
var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
$('.tabs .tab_content').hide(); // Hide all divs
$(currentTab).show(); // Show div with id equal to variable currentTab
return false;
});
这是示例 HTML:
<div class="box tabs">
<div class="box_header">
<h2>3/4 Width</h2>
<ul class="tab_nav">
<li><a href="#tab1" class="current_tab">Tab #1</a></li>
<li><a href="#tab2">Tab #2</a></li>
<li><a href="#tab3">Tab #3</a></li>
<li><a href="#tab4">Tab #4</a></li>
</ul>
</div>
<div class="box_content tab_content" id="tab1">1</div>
<div class="box_content tab_content" id="tab2">2</div>
<div class="box_content tab_content" id="tab3">3</div>
<div class="box_content tab_content" id="tab4">4</div>
</div>
它适用于一组选项卡,但如果我添加另一组(即上面的另一个代码块),它就会一团糟——它将选项卡视为一个大对象,而不是两个单独的选项卡实例。我怎样才能转换它以使其正常工作?理想情况下不向 HTML 添加太多/任何内容?
谢谢!
亚历克斯
【问题讨论】: