& # 39;长度# 39;在使用jQuery's inArray时,为null或不是IE中的对象

'length' is null or not an object in IE when using jQuery's inArray

本文关键字:null 对象 IE inArray 长度 jQuery      更新时间:2023-09-26

我正在使用jKey,但在IE8中我得到这个错误:

'length' is null or not an object

当我按某些键时。似乎每当我按下一个键,还不存在(就像如果我从来没有设置任何a它有错误信息)。但是,我确实为enter设置了一个关键命令,但它仍然在做。

导致错误的代码是:

 if($.inArray(e.keyCode,keySplit[x]) > -1){
  // Initiate the active variable
  var active = 'unchecked';
  // All the individual keys in the combo with the keys that are currently being pressed
  for(y in keySplit[x]) {
   if(active != false) {
    if($.inArray(keySplit[x][y], activeKeys) > -1){
     active = true;
    }
    else {
     active = false;
    }
   }
  }

错误发生在第一行。知道keySplit[x]返回typeof number当不存在或如果快捷键只有一个键,我添加:

if(typeof(keySplit[x]) == 'number'){ keySplit[x] = [keySplit[x]]; }

在上面代码块的正上方。

之后,它仍然在Chrome和FF中工作,但仍然不能在IE中工作。我也试过:

if(typeof(keySplit[x]) == 'number'){ keySplit[x] = new Array(keySplit[x]); }
if(typeof(keySplit[x]) == 'number'){ keySplit[x] = [keySplit[x].toString()]; }
if(typeof(keySplit[x]) == 'number'){ keySplit[x] = new Object(keySplit[x]); }

但是没有一个成功。导致错误的jQuery代码是:

inArray: function( elem, array ) {
    if ( indexOf ) {
        return indexOf.call( array, elem );
    }
    for ( var i = 0, length = array.length; i < length; i++ ) {
        if ( array[ i ] === elem ) {
            return i;
        }
    }
    return -1;
},

我认为,您的问题与另一个jKey错误有关。请检查我的答案 -你应该使用数组像遍历在你的实现,而不是关联。