我尝试为菜单中的每个链接添加一个切换按钮,以便在按下链接后在移动设备上关闭菜单。我通过向data-toggle="collapse" data-target="#navbarSupportedContent"
添加<a href=""?>
找到了解决方案。现在,此解决方案可以在引导程序3中使用,但在引导程序4中,它仍会关闭菜单,但不再链接到<a href="">
中的文本。
为什么这会阻止从链接到链接,我该如何解决?
以下小提琴说明了问题,我在没有切换的链接和有切换的链接上方添加了注释。忽略它周围的所有内容,我敢肯定它周围的任何代码中都没有。
代码:
.block{
height: 200px;
padding: 100px;
font-size: 30px;
border-top: 3px solid gray;
}
.navbar li a {
color: #bbb;
}
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto container px-md-5">
<!-- Links WITHOUT data-toggle -->
<li data-menuanchor="block1">
<a href="#block1">block1</a>
</li>
<li data-menuanchor="block2">
<a href="#block2">block2</a>
</li>
<li data-menuanchor="block3">
<a href="#block3">block3</a>
</li>
<!-- END of Links WITHOUT data-toggle -->
<!-- Links WITH data-toggle -->
<li data-menuanchor="block1">
<a href="#block1" data-toggle="collapse" data-target="#navbarSupportedContent">block1 (with toggle+target)</a>
</li>
<li data-menuanchor="block2">
<a href="#block2" data-toggle="collapse" data-target="#navbarSupportedContent">block2 (with toggle+target)</a>
</li>
<li data-menuanchor="block3">
<a href="#block3" data-toggle="collapse" data-target="#navbarSupportedContent">block3 (with toggle+target)</a>
</li>
<!-- END of Links WITH data-toggle -->
</ul>
</div>
</nav>
<a name="block1"></a>
<div class="block" data-anchor="block1">block1</div>
<a name="block2"></a>
<div class="block" data-anchor="block2">block2</div>
<a name="block3"></a>
<div class="block" data-anchor="block3">block3</div>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
我也愿意接受其他解决方案,例如JavaScript或html中的解决方案。
答案 0 :(得分:0)
尝试一下
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item dropdown">
<a class="nav-link" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Menu Name 1 </a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a href="#block1" class="dropdown-item">Submenu 1-1</a>
<a href="#block2" class="dropdown-item"> Submenu 1-2</a>
<a href="#block3" class="dropdown-item">Submenu 1-3</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Menu Name 2 </a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a href="#link1" class="dropdown-item"> Submenu 2-1</a>
<a href="#link2" class="dropdown-item"> Submenu 2-2</a>
<a href="#link3" class="dropdown-item"> Submenu 2-3</a>
</div>
</li>
</ul>
</div>