twitter-bootstrap 带有标签的 Twitter Bootstrap 下拉菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21525440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Twitter Bootstrap Dropdown with Tabs inside
提问by tim peterson
I'm trying to make a dropdown menu with tabs inside of it. The problem is the dropdown closes when I click a tab. See this demo: http://jsfiddle.net/timrpeterson/pv2Lc/2/. (code reproduced below).
我正在尝试制作一个带有标签的下拉菜单。问题是当我单击选项卡时下拉菜单关闭。请参阅此演示:http: //jsfiddle.net/timrpeterson/pv2Lc/2/。(代码复制如下)。
The cited suggestion to stopPropagation()on the tabs anchors doesn't seem to help. Is there a better way?
在列举建议,stopPropagation()在标签上锚似乎并没有帮助呢。有没有更好的办法?
<script src='http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js'></script>
<link href='http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css' rel="stylesheet" type="text/css"/>
<div class="dropdown">
<a id="dLabel" role="button" data-toggle="dropdown" data-target="#" href="/page.html">
Dropdown <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
<ul class="nav nav-tabs">
<li class="active"><a href="#home" data-toggle="tab">Home</a> </li>
<li><a href="#profile" data-toggle="tab">Profile</a></li>
<li><a href="#messages" data-toggle="tab">Messages</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane active" id="home">HOME asdfasdfsda</div>
<div class="tab-pane" id="profile">PROFILE asdfafas</div>
<div class="tab-pane" id="messages">MESSAGES asdfdas</div>
</div>
</ul>
</div>
回答by gpgekko
The cited suggestion doesn't work because the .tab('show')method should be invoked on the actual tab (i.e., the link) instead of the tab pane.
引用的建议不起作用,因为.tab('show')应该在实际选项卡(即链接)而不是选项卡窗格上调用该方法。
So the code should be:
所以代码应该是:
$('.dropdown-menu a[data-toggle="tab"]').click(function (e) {
e.stopPropagation()
$(this).tab('show')
})
Here is an updated fiddle: http://jsfiddle.net/pv2Lc/6/
这是一个更新的小提琴:http: //jsfiddle.net/pv2Lc/6/
回答by LeGEC
Use the specific bootstrap events : the hide.bs.dropdown(triggered when the dropdown is about to close) allows you to cancel the "hide" action by calling e.preventDefault(). (doc here, list of specific bootstrap events for scrolldowncomponents)
使用特定的引导事件:(在hide.bs.dropdown下拉列表即将关闭时触发)允许您通过调用e.preventDefault(). (此处的文档,scrolldown组件的特定引导事件列表)
You can activate a special flag when the user clicks on a tab, and check that flag in the hide.bs.dropdownevent on the dropdown :
您可以在用户单击选项卡时激活一个特殊标志,并hide.bs.dropdown在下拉列表的事件中检查该标志:
$('#myTabs').on('click', '.nav-tabs a', function(){
// set a special class on the '.dropdown' element
$(this).closest('.dropdown').addClass('dontClose');
})
$('#myDropDown').on('hide.bs.dropdown', function(e) {
if ( $(this).hasClass('dontClose') ){
e.preventDefault();
}
$(this).removeClass('dontClose');
});
fiddle
(I added html ids to your example, change the selectors according to your needs)
小提琴
(我在您的示例中添加了 html id,根据您的需要更改选择器)

