我想要做的就是在同一页面上有多个选项卡区域,并使用 JS 中的一个类来处理它们。我不想手动向 JS 添加单独的 ID 来激活选项卡。
这是一个小提琴:http: //jsfiddle.net/V7a2C/2/
这是我的代码:
HTML
<aside id="0" class="widget tabbed popular-articles">
<header>
<h1 class="widget-title">Popular Articles 1</h1>
<a href="#" class="more">More</a>
<ul class="tab-nav">
<li class="active"><a href="#tab1">Read</a></li>
<li><a href="#tab2">Emailed</a></li>
</ul>
</header>
<div class="content">
<div id="tab1" class="tab read first active">
<ol>
<li>
<a href="#">The IT Talent Problem</a>
</li>
<li>
<a href="#">Finance Leaders Bemoan Talent Shortage</a>
</li>
<li>
<a href="#">A Powerful Stock-Price Predictor</a>
</li>
<li>
<a href="#">The Great Pension Derisking</a>
</li>
<li>
<a href="#">It's All in the Game</a>
</li>
</ol>
</div>
<div id="tab2" class="tab emailed second">
<ol>
<li>
<a href="#">Special Report: Benchmarking Tech</a>
</li>
<li>
<a href="#">The IT Talent Problem</a>
</li>
<li>
<a href="#">The Great Pension Derisking</a>
</li>
<li>
<a href="#">A Powerful Stock-Price Predictor</a>
</li>
<li>
<a href="#">It's All in the Game</a>
</li>
</ol>
</div>
</div>
</aside>
<aside id="1" class="widget tabbed popular-articles">
<header>
<h1 class="widget-title">Popular Articles 2</h1>
<a href="#" class="more">More</a>
<ul class="tab-nav">
<li class="active"><a href="#tab1">Read</a></li>
<li><a href="#tab2">Emailed</a></li>
</ul>
</header>
<div class="content">
<div id="tab1b" class="tab read first active">
<ol>
<li>
<a href="#">The IT Talent Problem</a>
</li>
<li>
<a href="#">Finance Leaders Bemoan Talent Shortage</a>
</li>
<li>
<a href="#">A Powerful Stock-Price Predictor</a>
</li>
<li>
<a href="#">The Great Pension Derisking</a>
</li>
<li>
<a href="#">It's All in the Game</a>
</li>
</ol>
</div>
<div id="tab2b" class="tab emailed second">
<ol>
<li>
<a href="#">Special Report: Benchmarking Tech</a>
</li>
<li>
<a href="#">The IT Talent Problem</a>
</li>
<li>
<a href="#">The Great Pension Derisking</a>
</li>
<li>
<a href="#">A Powerful Stock-Price Predictor</a>
</li>
<li>
<a href="#">It's All in the Game</a>
</li>
</ol>
</div>
</div>
</aside>
JS
$('.tab-nav li a').click(function(){
var currentTab = $(this).attr('href');
var tabs = $(this).parents('aside');
$(this).parent('li').siblings().removeClass('active');
$(this).parent('li').addClass('active');
tabs.find('.tab').removeClass('active');
$(currentTab).addClass('active');
console.log(tabs.find('.tab'));
var tabs = $(this).parents('aside');
return false;
});