JS取消默认事件的方法是什么

JS取消默认事件的方法有两种:

使用event.preventDefault()方法:在事件处理函数中调用event.preventDefault()方法可以阻止事件的默认行为。例如,当点击一个链接时,调用event.preventDefault()可以阻止页面跳转。

document.querySelector('a').addEventListener('click', function(event) {
event.preventDefault(); // 取消默认事件
});

使用return false语句:在事件处理函数中使用return false语句也可以取消默认事件。这种方式通常用于HTML中的事件处理函数。

<a href="#" onclick="return false;">取消默认事件</a>
阅读剩余
THE END