twitter-bootstrap 以编程方式切换引导程序 3 导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21675336/
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
Programmatically toggle bootstrap 3 navigation bar
提问by bboydflo
I am using few items in my bootstrap 3 navigation bar grouped in like below:
我在 bootstrap 3 导航栏中使用了几个项目,如下所示:
<nav class="navbar navbar-inverse navbar-fixed-top" id="toggleNav" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand name</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="javascript:logout();"><span class="glyphicon glyphicon-log-out"></span> Logout</a>
</li>
<li>
<a href="#" id="online-offline" toggle="offline"><span class="glyphicon glyphicon-off"></span> Go offline</a>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
When I click on the second item ('Go offline') I use jquery to invoke a modal window. On mobile I have to first collapse the menu to reach the link, and then when I click on that item I get my modal window correctly.
当我点击第二个项目(“离线”)时,我使用 jquery 来调用一个模态窗口。在移动设备上,我必须首先折叠菜单才能到达链接,然后当我单击该项目时,我才能正确地获得我的模态窗口。
What I need to achieve is to hide the navbar collapse before showing the modal. Is it possible to programmatically toggle the navbar collapse? How can I do that ?
我需要实现的是在显示模态之前隐藏导航栏折叠。是否可以以编程方式切换导航栏折叠?我怎样才能做到这一点 ?
回答by BENARD Patrick
As you said, the event will run a modal.
正如您所说,该事件将运行一个模态。
So, when your modal ( called yourModal ) is showing (before showing), just hide the menu :
因此,当您的模式(称为 yourModal )显示时(显示之前),只需隐藏菜单:
JS :
JS:
$('.yourModal').on('show.bs.modal', function(){
$('.navbar-collapse').collapse('hide');
});
Here are the docs :
以下是文档:
http://getbootstrap.com/javascript/#collapse
http://getbootstrap.com/javascript/#collapse
回答by bboydflo
After a little bit of help from @Jahnux73 I figured it out myself. So the only thing I had to do is to add :
在@Jahnux73 的帮助下,我自己弄明白了。所以我唯一要做的就是添加:
data-toggle="collapse" data-target=".navbar-ex1-collapse"
to the specific link that I wanted to toggle the navbar. so the link now looks like following:
到我想切换导航栏的特定链接。所以链接现在如下所示:
<a href="#" id="online-offline" data-toggle="collapse"
data-target=".navbar-ex1-collapse" toggle="offline">
<span class="glyphicon glyphicon-off"></span> Go offline
</a>
回答by HamidReza
add custom class to toggle navbar (me-toggle) so toggle variable with true value :
添加自定义类以切换导航栏(me-toggle),以便使用真值切换变量:
<a href="javascript:;" class="dropdown-toggle me-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true">
<i class="icon-envelope-open"></i>
<span class="badge badge-default"> 4 </span>
</a>
<ul class="dropdown-menu ">
<li class="external">
<h3>You have<span class="bold">7 New</span> Messages</h3>
<a href="app_inbox.html">view all</a>
</li>
<li>
item 1
</li>
<li>
item 2
</li>
</ul>
then in click navbar event :
然后在单击导航栏事件中:
toggle: true;
$('a.me-toggle').click(() => {
toggle = !toggle;
});
$('.dropdown').on({
"click": function(event) {
},
"hide.bs.dropdown": function(event) {
return toggle;
}
});
回答by Konstantin XFlash Stratigenas
Just use:
只需使用:
$('.collapse').collapse();
回答by Koba
First, add an id to your button tag.
首先,为您的按钮标签添加一个 id。
<button id="idToggleButton" type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
Than add an id to your collapse div
比给你的折叠 div 添加一个 id
<div id="divCollapse" class="collapse navbar-collapse navbar-ex1-collapse">
Finally you can programmatically click when divCollapse is visible
最后,您可以在 divCollapse 可见时以编程方式单击
if ($('#divCollapse').is(":visible")){
$('#idToggleButton').click();
}
Or when is invisible
或者什么时候看不见
if (!$('#divCollapse').is(":visible")){
$('#idToggleButton').click();
}

