简体   繁体   English

Firefox中的事件“ e未定义”

[英]event in firefox “e is undefined”

I have a function that get the key code when pressed. 我有一个可以在按下时获取键码的功能。

Here is my code: 这是我的代码:

function Getkeycode(e){
   var keycode = null;
   if (window.event) 
       keycode = window.event.keyCode;
   else 
       keycode = e.which;
   return keycode;
}

window.onkeydown = function(){
   alert(Getkeycode());  // I'm tried to using  Getkeycode() or Getkeycode(e) but still error
}

Previous code works well in Internet Explorer, but in Firefox always occurs error message >> e is undefined or e is not defined 先前的代码在Internet Explorer中运行良好,但在Firefox中始终会出现错误消息>> e is undefinede is not defined

In IE, when an event occurs it is globally accessible in a window variable. 在IE中,当事件发生时,可以在窗口变量中全局访问它。 That's not the case in other browsers. 在其他浏览器中则不是这种情况。

In order to pass the event to your function, change your code to 为了将事件传递给函数,请将代码更改为

window.onkeydown = function(e){ // <== receive e
   alert(Getkeycode(e));  // <== pass e
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM