<!DOCTYPE html>
<html>
<head>
<script>
function whichButton() {
document.getElementById("demo").innerHTML = event.keyCode;
}
</script>
</head>
<body onkeyup="whichButton()">
<p><b>Note:</b> Make sure the right frame has focus when trying this example!</p>
<p>Click on this page, and press a key on your keyboard.</p>
<p id="demo"></p>
</body>
</html>
以上是我修改的w3schools.com的代码。
原始代码的参数“event”如下所示:whichButton(event)
我想知道该参数“event”的作用是什么以及为什么人们插入该“event”参数。
我从上面的代码中删除了“event”参数,但它仍然工作正常。
我认为代码没有错误。一定有什么我不知道的事情......
最佳答案
隐式事件
并非在所有浏览器中都有效,不应使用。我的猜测是出于兼容性原因,event
被添加到某些浏览器中。
以下测试告诉我们它们是否是对同一对象的引用:
document.body.addEventListener("keyup", function (e) { alert(e === event); });
Internet Explorer 11:true
Google Chrome 39:true
Mozilla Firefox 33:ReferenceError:事件未定义
关于javascript - 插入 'event'与不插入有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26409356/