仅当您单击十字按钮时,我的菜单才会关闭。但这在登陆页面上并不方便。我希望当我单击菜单中的任何链接时它会关闭。我尝试自己添加代码,但它对我不起作用。我尝试添加 querySelector,它仅适用于主页链接。其他链接没有关闭菜单。我究竟做错了什么?
let
burger = document.getElementById("burger"),
nav = document.getElementById("main-nav"),
a = document.getElementsByClassName(".nav-link");
burger.addEventListener("click", function(e) {
this.classList.toggle("is-open");
nav.classList.toggle("is-open");
});
a.addEventListener("click", function(e) {
burger.classList.toggle("is-open");
nav.classList.toggle("is-open");
});
<button id="burger" class="navbar-toggler open-main-nav"><span class="burger"></span></button>
<nav class="main-nav" id="main-nav">
<ul>
<li><a href="#home" class="nav-link" title="">home</a></li>
<li><a href="#services" class="nav-link" title="">services</a></li>
<li><a href="#portfolio" class="nav-link" title="">portfolio</a></li>
<li><a href="#about" class="nav-link" title="">about</a></li>
<li><a href="#contacts" class="nav-link" title="">contacts</a></li>
</ul>
</nav>
Smart猫小萌
相关分类