问题
Consider the following code:
<input id="myinput" type="text" size="40" onkeydown="myFunction()">
function myFunction() {
console.log(event);
}
event is a global object and is known inside the event handler myFunction. However, Firefox throws an error:event is not defined while Chrome and IE output KeyboardEvent {...}. This is one of the most crazy things I have met. Any satisfactory explanation?
回答1:
Firefox's KeyboardEvent() expect event passed to the function Try this
<input id="myinput" type="text" size="40" onkeydown="myFunction(event)">
function myFunction(event){
if(typeof event === 'undefined')
{
event = window.event;
}
console.log(event);
}
来源:https://stackoverflow.com/questions/37367752/event-global-object-in-firefox