这适用于 Chrome:
<a href="#" onclick="myFunction()">Aaa</a>
function myFunction() {
event.preventDefault();
//.........
但是在FireFox中事件没有定义。如何在不使用 jquery 的情况下让它在 Firefox 中工作?
更新:
function myFunction(event) {
event.preventDefault();
}
如果事件没有从 onclick 传递给 myFunction,它将如何到达 myFunction?
最佳答案
你缺少一个参数,即事件。
function myFunction(event) {
event.preventDefault();
console.log("It worked");
}
此外,在 anchor 标记的 onclick
属性中,将事件作为参数传递:
<a href="#" onclick="myFunction(event)">Aaa</a>
为了验证它是否解决了问题,这里有一个可运行的代码片段:
<a href="#" onclick="myFunction(event)">Aaa</a>
<script>
function myFunction(event) {
event.preventDefault();
console.log("It worked");
}
</script>
关于javascript - event.preventDefault——在 Firefox 中,没有 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501673/