我有一个导航标签的Bootstrap实现,加载页面时没有活动标签。但是,我想让每个标签的功能都作为一个切换工作,这样如果一个标签是"活跃的"它会丢失该类并默认为页面的原始状态。
这是我的HTML:
<div class="well nav-base">
<div class="pull-right"><a href="#">collapse all tabs</a></div>
<ul class="nav nav-tabs" id="testtab">
<li><a href="#home" data-toggle="tab">Home</a></li>
<li><a href="#profile" data-toggle="tab">Profile</a></li>
<li class="dropdown">
<a href="#" id="myTabDrop1" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li><a href="#dropdown1" tabindex="-1" data-toggle="tab">@fat</a></li>
<li><a href="#dropdown2" tabindex="-1" data-toggle="tab">@mdo</a></li>
</ul>
</li>
</ul>
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade" id="home">
<p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p>
</div>
<div class="tab-pane fade" id="profile">
<p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p>
</div>
<div class="tab-pane fade" id="dropdown1">
<p>Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade. Messenger bag gentrify pitchfork tattooed craft beer, iphone skateboard locavore carles etsy salvia banksy hoodie helvetica. DIY synth PBR banksy irony. Leggings gentrify squid 8-bit cred pitchfork. Williamsburg banh mi whatever gluten-free, carles pitchfork biodiesel fixie etsy retro mlkshk vice blog. Scenester cred you probably haven't heard of them, vinyl craft beer blog stumptown. Pitchfork sustainable tofu synth chambray yr.</p>
</div>
<div class="tab-pane fade" id="dropdown2">
<p>Trust fund seitan letterpress, keytar raw denim keffiyeh etsy art party before they sold out master cleanse gluten-free squid scenester freegan cosby sweater. Fanny pack portland seitan DIY, art party locavore wolf cliche high life echo park Austin. Cred vinyl keffiyeh DIY salvia PBR, banh mi before they sold out farm-to-table VHS viral locavore cosby sweater. Lomo wolf viral, mustache readymade thundercats keffiyeh craft beer marfa ethical. Wolf salvia freegan, sartorial keffiyeh echo park vegan.</p>
</div>
</div>
</div><!--/.well -->
这是jQuery:
$(function(){
$("li.active a").click(function(){
var $parent = $(this).parent();
if (!$parent.hasClass("active")){
$parent.addClass("active");
event.stopImmediatePropagation();
}
});
});
我已经通过谷歌的深处搜索到了Bootstrap自己的网站,并且没有足够的结果。任何帮助表示赞赏。
答案 0 :(得分:1)
我改变了你的JavaScript。基本上,您要做两件事:从选项卡按钮中删除.active
类,并找到相应的选项卡内容并隐藏它。这就是我想出的:
$(function(){
$(document).on("click", "li.active > a", function(){
var $parent = $(this).parent();
// Ensure link isn't just a dropdown menu link
if (!$parent.hasClass("dropdown")){
// Deactivate tab buttons
$("#testtab li").removeClass("active");
// Hides tab contents
tabName = $(this).attr("href");
$(tabName).removeClass("active").addClass("fade");
}
});
});
我使用on()
函数更改了回调函数的分配方式 - 使用click()
不会考虑获得<li>
的任何.active
元素稍后将类应用于它们,只是那些目前拥有该类的类。
此外,在菜单中有一个下拉菜单,因此必须确保点击的链接不仅仅是下拉菜单链接,而且实际上是一个菜单项。我没有从.active
只移除<li>
类,而是发现有必要将其从导航菜单中的每个项目中删除,以确保下拉列表及其菜单项都已停用。
这里有JSFiddle来演示代码的实际效果。希望这可以帮助!如果您有任何问题,请告诉我。