我想单击链接时关闭菜单。你知道我的代码为什么不起作用吗? jsbin单击链接上的关闭菜单
document.getElementById("menu").addEventListener("click",function(e) {
if(e.target && e.target.nodeName == "LI") {
console.log("ok");
menu.style.display = "none";
}
});
<ul class="nav__right" id="menu">
<li>
<h3><a href="#home">home</a></h3>
</li>
<li>
<h3><a href="#about">À propos</a></h3>
</li>
<li>
<h3><a href="#production">Réalisations</a></h3>
</li>
<li>
<h3><a href="#contact">Contact</a></h3>
</li>
</ul>
你想,如果它被点击隐藏'li'元素? –
尝试'e.stopPropagation();' – Fr33d0m
@ Fr33d0m问题是条件没有得到满足。 –