Javascript 点击事件停止锚点 href link
Javascript click event stop anchor href link
当包含 js click 事件时,锚标记 href 不起作用
HTML:
<li id="serviciosbtn"><a href="#servicios">SERVICIOS</a></li>
JS
var servicios = document.getElementById("serviciosbtn");
servicios.addEventListener("click", function(e) {
nav.classList.add("hide-mobile");
e.preventDefault();
});
实际上应用了 class 但 a href 现在不起作用
@ullfindsmit,是的,当我删除 e.preventDefault() 时它起作用了
e.preventDefault();是唯一的原因
Read the official documenation
Try removing the e.preventDefault();
it will behave normally
当包含 js click 事件时,锚标记 href 不起作用 HTML:
<li id="serviciosbtn"><a href="#servicios">SERVICIOS</a></li>
JS
var servicios = document.getElementById("serviciosbtn");
servicios.addEventListener("click", function(e) {
nav.classList.add("hide-mobile");
e.preventDefault();
});
实际上应用了 class 但 a href 现在不起作用
@ullfindsmit,是的,当我删除 e.preventDefault() 时它起作用了
e.preventDefault();是唯一的原因 Read the official documenation
Try removing the e.preventDefault(); it will behave normally