1 回答
TA贡献2011条经验 获得超2个赞
你可以用Javascript它。
document.querySelectorAll('.custom_collapse').forEach(cur => {
cur.addEventListener('click', () => {
document.querySelector('.navbar_collapse').classList.remove('show');
});
});
#targetId {
/* JUST FOR SEE TARGET */
margin-top: 120vh;
}
<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 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 custom_collapse" href="#targetId">nav-link does get hidden, but it doesn't hit the target</a>
<a class="dropdown-item custom_collapse" 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>
- 1 回答
- 0 关注
- 117 浏览
添加回答
举报