我试图留在一个特定的选项卡上(在本例中为 tab2 ),但在单击提交按钮后,选项卡将恢复为选项卡 1,该选项卡具有默认的活动类。我用这个撞了一堵砖墙。
这是我的示例[jsfiddle]:
HTML:
<ul class='tabs'>
<li><a href='#tab1'>Tab 1</a></li>
<li><a href='#tab2'>Tab 2</a></li>
</ul>
<div id='tab1'>
<h3>Section 1</h3>
<p>Lorem ipsum dolor sit amet, consssectetur adipiscing elit. Donec lobortis placerat dolor id aliquet. Sed a orci in justo blandit commodo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae.</p>
<form id="form1" action="" method="post">
<input id="button-1" name="" type="submit" />
</form>
</div>
<div id='tab2'>
<a name="tab2"></a>
<h3>Section 2</h3>
<p>Aenean et est tortor. In pharetra pretium convallis. Mauris sollicitudin ligula non mi hendrerit varius. Fusce convallis hendrerit mauris, eu accumsan nisl aliquam eu.</p>
<form id="form2" action="" method="post">
<input id="button-2" name="" type="submit" />
</form>
</div>
<div>
<h2>RESULTS </h2>
</div>
JavaScript:
$(document).ready(function(){
$('ul.tabs').each(function(){
var $active, $content, $links = $(this).find('a');
$active = $links.first().addClass('active');
$content = $($active.attr('href'));
$links.not(':first').each(function () {
$($(this).attr('href')).hide();
});
$(this).on('click', 'a', function(e){
$active.removeClass('active');
$content.hide();
$active = $(this);
$content = $($(this).attr('href'));
$active.addClass('active');
$content.show();
e.preventDefault();
});
});
});
CSS:
* {padding:0; margin:0;}
html {padding:15px 15px 0;font-family:sans-serif;font-size:14px;}
p, h3 {margin-bottom:15px;}
div {padding:10px;width:600px;background:#fff;}
ul.tabs {margin-left:2px;}
#tab1, #tab2 {border-top: solid 1px #ccc;margin-top: -1px; }
#tab2 {display:none;}a
ul.tabs li {list-style:none;display:inline;}
ul.tabs li a {padding:5px 10px;display:inline-block;background:#666;color:#fff;text-decoration:none;}
ul.tabs li a.active {background:#fff;color:#000;border: solid 1px #ccc; border-width: 1px 1px 0 1px;}
</p>
您的帮助将不胜感激