以下是我想要实现的目标 - 当选择导航栏中的下拉项时,完成以下两个操作:
转到目标 id 的 div
将导航栏卷起至初始状态(将其隐藏回完全卷起的状态)
事实证明,我只能做到其中之一,但不能同时做到两者。有人可以帮助指导我如何实现这两点吗?我相信第一个下拉项锚点应该做到这一点,但是,由于某种原因,它在卷起导航栏后不会转到 targetId。data-toggle="collapse"
仅当我删除和 时,锚点才会带我到达目标data-target=".navbar-collapse.show"
,这是第二个锚点显示的内容,但导航栏不会卷起。
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<nav class="navbar navbar-dark bg-dark fixed-top">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Menu</a>
<div class="dropdown-menu shadow rounded" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#targetId" data-toggle="collapse" data-target=".navbar-collapse.show">nav-link does get hidden, but it doesn't hit the target</a>
<a class="dropdown-item" href="#targetId">Hits the target, but the nav-link does not get hidden</a>
</div>
</li>
</ul>
</div>
</nav>
<!-- navbar ends here -->
<div id="targetId">Content</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
森林海
相关分类