简体   繁体   English

函数表达式/事件检测在ie <= 9或Safari中不起作用

[英]function expression/event detection not working in ie<=9 or Safari

I've been searching for an answer to this for about a day now, I have a function that just won't work in IE9 or below or Safari. 我一直在寻找答案的问题已有大约一天,但我有一个功能仅在IE9或更低版本或Safari中无法使用。 I think it's because it's triggered by a keyboard event. 我认为这是因为它是由键盘事件触发的。 It also passes the event to the function so onkeyup the function can check the value and act accordingly. 它还将事件传递给函数,以便onkeyup函数可以检查值并采取相应的措施。

I was wonder if anyone could enlighten me as to what the problem is, the function is as follows: 我想知道是否有人可以启发我解决问题所在,其功能如下:

window.onkeyup = function(e) { //This doesn't seem to work in the above browsers

   var key = e.keyCode ? e.keyCode : e.which; //But neither does this

   if (key == 83 && array.length>=0) {

        // Log answer as 'S'

   }else if (key == 68 && array.length>=0) {

        //Log answer as 'D'

   }

}   

Any help would be appreciated, thank you. 任何帮助,将不胜感激,谢谢。

Could be because array is not defined in the code, at least in the part of code is visible here. 可能是因为未在代码中定义array ,因此至少在此部分的代码中可见。 Try invert the condition, maybe will works then. 尝试反转条件,也许会起作用。

(e.which) ? e.which : e.keyCode

Other idea is use the code below before use e in the previous comparisons: 其他想法是在之前的比较中使用e之前使用以下代码:

 if (window.event){
   e = window.event.keyCode;}
   else if (event.charCode) {
    e = event.charCode;}
   else if (event.which){
   e = event.which; } 
 }

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

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