我希望防止HTML按钮像链接那样将您带到同一个页面重新加载该页面
例如:如果你在主页上,不要在点击徽标时重新加载网站。
我该怎么做?
完整代码:https://codepen.io/Muh-Ail/pen/oNpLKXM?editors=0010
let activeBtn = document.querySelector(".active-btn");
let currentLocation = location.href;
if (activeBtn.href === currentLocation) {
activeBtn.preventDefault();
}
发布于 2022-03-21 08:29:27
用这个代替
let activeBtn = document.querySelector(".active-btn");
let currentLocation = location.href;
if (activeBtn.href === currentLocation) {
activeBtn.addEventListener('click',function(e){
e.preventDefault();
});
}
发布于 2022-03-21 08:27:09
您必须使用来自侦听器ele.addEventListener("click", function (e)
的ele.addEventListener("click", function (e)
;
另外,您应该用以下内容包装js:
document.addEventListener("DOMContentLoaded", () => {
// code here
});
您应该添加不同的链接,使您能够看到。
https://stackoverflow.com/questions/71560478
复制