Check out the fiddle. I have a basic Bootstrap 3 responsive nav like so:
看看小提琴。我有一个基本的Bootstrap 3响应导航,如下所示:
<div id="navbar" class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<a id="navbar-toggle" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="navbar-brand" href="#">Title Title Title</a>
<div id="nav-collapse" class="nav-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="#option1">Option 1</a></li>
<li><a href="#option2">Option 2</a></li>
<li><a href="#option3">Option 3</a></li>
</ul>
</div>
</div>
</div>
The nav items link to sections on the page, rather than different pages so I need the dropdown to hide on click.
导航项链接到页面上的部分,而不是不同的页面,所以我需要下拉隐藏点击。
Whenever I try to toggle the dropdown manually with jQuery, it messes up future functionality of the dropdown toggle button:
每当我尝试使用jQuery手动切换下拉列表时,它会混淆下拉切换按钮的未来功能:
$("#navbar li a").click(function(event) {
// check if window is small enough so dropdown is created
$("#navbar-toggle").is(":visible")
$("#nav-collapse").toggle();
});
Is there a better fix for this?
有更好的解决方案吗?
5 个解决方案
#1
24
If you wanted to do this without JavaScript you could simply add the data-target and data-toggle to each list item, like so:
如果你想在没有JavaScript的情况下这样做,你可以简单地将数据目标和数据切换添加到每个列表项,如下所示:
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#option1">Option 1</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#option2">Option 2</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#option3">Option 3</a></li>
For the navbar, this really only works in the mobile view mode when there is a toggle button. Strange things happen when the navbar is exposed (the toggle button isn't showing).
对于导航栏,当有切换按钮时,这实际上仅适用于移动视图模式。暴露导航栏时会发生奇怪的事情(切换按钮未显示)。
#2
8
Change this:
$(document).ready(function () {
$("#navbar li a").click(function(event) {
// check if window is small enough so dropdown is created
$("#navbar-toggle").is(":visible")
$("#nav-collapse").toggle();
});
});
to this:
$(document).ready(function () {
$("#navbar li a").click(function(event) {
// check if window is small enough so dropdown is created
$("#nav-collapse").removeClass("in").addClass("collapse");
});
});
#3
3
I don't know why clicking/tapping on a menu item in a menu dropdown in a collapsed navbar doesn't automatically toggle the menu in Bootstrap. There may be a more graceful way to do this, but, here's how I fixed it (using Bootstrap 3.0.3):
我不知道为什么点击/点击折叠导航栏菜单下拉列表中的菜单项不会自动切换Bootstrap中的菜单。可能有更优雅的方式来做到这一点,但是,这是我如何修复它(使用Bootstrap 3.0.3):
<li class="visible-xs">
<a href="#" data-toggle="collapse" data-target=".navbar-collapse">Link</a>
</li>
<li class="hidden-xs">
<a href="#">Link</a>
</li>
Basically, there are two versions of the link, one that appears whenever the menu toggle is shown (at browser widths <768px) that will toggle the navbar menu, and one that doesn't collapse it (browser widths >768px). If you don't add that second type, it will show a strange phantom animated horizontal scroll bar when trying to toggle the menu when it is not there.
基本上,链接有两个版本,每当显示菜单切换时(浏览器宽度<768px)将切换导航栏菜单,并且不会折叠它(浏览器宽度> 768px)。如果你没有添加第二种类型,它会在尝试切换菜单时显示一个奇怪的幻影动画水平滚动条。
#4
2
I am new to Bootstrap but with a small changed to the above code it works beautifully now. Remember that when you click on the logo the drop-down menu should close too, that is why I have added navbar-header to it!
我是Bootstrap的新手,但是对上面的代码进行了一些改动后,它现在可以很好地工作了。请记住,当您点击徽标时,下拉菜单也会关闭,这就是我为其添加了navbar-header的原因!
jQuery(document).ready(function () {
jQuery(".nav a, .navbar-header a").click(function(event) {
// check if window is small enough so dropdown is created
jQuery(".navbar-collapse").removeClass("in").addClass("collapse");
});
});
#5
2
With Bootstrap 3.3.4 I realized my old way quit working. Updated it to:
使用Bootstrap 3.3.4我意识到我的旧方式退出工作。更新为:
if ($('div.navbar-collapse').hasClass('in')) {
$('button.navbar-toggle:visible').click();
}
#1
24
If you wanted to do this without JavaScript you could simply add the data-target and data-toggle to each list item, like so:
如果你想在没有JavaScript的情况下这样做,你可以简单地将数据目标和数据切换添加到每个列表项,如下所示:
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#option1">Option 1</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#option2">Option 2</a></li>
<li data-toggle="collapse" data-target=".nav-collapse"><a href="#option3">Option 3</a></li>
For the navbar, this really only works in the mobile view mode when there is a toggle button. Strange things happen when the navbar is exposed (the toggle button isn't showing).
对于导航栏,当有切换按钮时,这实际上仅适用于移动视图模式。暴露导航栏时会发生奇怪的事情(切换按钮未显示)。
#2
8
Change this:
$(document).ready(function () {
$("#navbar li a").click(function(event) {
// check if window is small enough so dropdown is created
$("#navbar-toggle").is(":visible")
$("#nav-collapse").toggle();
});
});
to this:
$(document).ready(function () {
$("#navbar li a").click(function(event) {
// check if window is small enough so dropdown is created
$("#nav-collapse").removeClass("in").addClass("collapse");
});
});
#3
3
I don't know why clicking/tapping on a menu item in a menu dropdown in a collapsed navbar doesn't automatically toggle the menu in Bootstrap. There may be a more graceful way to do this, but, here's how I fixed it (using Bootstrap 3.0.3):
我不知道为什么点击/点击折叠导航栏菜单下拉列表中的菜单项不会自动切换Bootstrap中的菜单。可能有更优雅的方式来做到这一点,但是,这是我如何修复它(使用Bootstrap 3.0.3):
<li class="visible-xs">
<a href="#" data-toggle="collapse" data-target=".navbar-collapse">Link</a>
</li>
<li class="hidden-xs">
<a href="#">Link</a>
</li>
Basically, there are two versions of the link, one that appears whenever the menu toggle is shown (at browser widths <768px) that will toggle the navbar menu, and one that doesn't collapse it (browser widths >768px). If you don't add that second type, it will show a strange phantom animated horizontal scroll bar when trying to toggle the menu when it is not there.
基本上,链接有两个版本,每当显示菜单切换时(浏览器宽度<768px)将切换导航栏菜单,并且不会折叠它(浏览器宽度> 768px)。如果你没有添加第二种类型,它会在尝试切换菜单时显示一个奇怪的幻影动画水平滚动条。
#4
2
I am new to Bootstrap but with a small changed to the above code it works beautifully now. Remember that when you click on the logo the drop-down menu should close too, that is why I have added navbar-header to it!
我是Bootstrap的新手,但是对上面的代码进行了一些改动后,它现在可以很好地工作了。请记住,当您点击徽标时,下拉菜单也会关闭,这就是我为其添加了navbar-header的原因!
jQuery(document).ready(function () {
jQuery(".nav a, .navbar-header a").click(function(event) {
// check if window is small enough so dropdown is created
jQuery(".navbar-collapse").removeClass("in").addClass("collapse");
});
});
#5
2
With Bootstrap 3.3.4 I realized my old way quit working. Updated it to:
使用Bootstrap 3.3.4我意识到我的旧方式退出工作。更新为:
if ($('div.navbar-collapse').hasClass('in')) {
$('button.navbar-toggle:visible').click();
}