<!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的代码。插入“事件”与否有什么区别?
原代码有参数“事件”是这样的:whichButton(事件)
我不知道那是什么参数“事件”,为什么人们插入“事件”参数的功能。
我从上面的代码删除了'event'参数,但它仍然正常工作。
我认为代码没有错。必须有我不知道的东西...
在某些浏览器中,'event'引用全局变量(window对象的一个属性)。在其他情况下,事件对象作为参数传递给事件处理程序,而不是全局变量。在Firefox中试用你的代码。 – Pointy 2014-10-16 16:26:25