jQuery - Object在IE8中不支持此属性或方法

Ekt*_*tel 2 jquery internet-explorer-8

我在我的一个项目中实现了便签功能,我发现了与浏览器兼容性相关的问题.我的代码在IE8中不起作用,但在其他浏览器中工作正常.

我得到的错误是:

Message: Object doesn't support this property or method
Line: 45
Char: 3
Code: 0
URI: http://dev.mesocial.co/orange_theme/js/sticky_note_func.js
Run Code Online (Sandbox Code Playgroud)

这些是40到50之间的界限:

var moved = function(note) {

    // added by rupesh 
    // alert(JSON.stringify(lastCreatedNoteId)); 
    var passId = note.id
    if(lastCreatedNoteIdForJs.indexOf(note.id) != -1)
         passId = lastCreatedNoteId[note.id];
    // till here ///////
    $.post(SITE_URL+'/dashboard/create-sticky-note/act/moved/sticky_note_id/'+passId+'/pos_x/'+note.pos_x+'/pos_y/'+note.pos_y,
    function(data) {
        //alert(data);
    });
Run Code Online (Sandbox Code Playgroud)

任何有关问题的帮助都将非常感激.

Chr*_*son 7

IE8不支持.indexOf().在Internet Explorer中,它首次引入了IE9.

由于您使用的是jQuery,因此可以使用该$.inArray()方法.

在你的情况下,像这样的东西:

if($.inArray(note.id, lastCreatedNoteIdForJs) != -1)
Run Code Online (Sandbox Code Playgroud)

支持没有jQuery的旧版浏览器:

如果您也想.indexOf()在旧版浏览器中使用,则需要时使用自己的数组原型.原型看起来像这样:

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
    "use strict";
    if (this == null) {
      throw new TypeError();
    }
    var t = Object(this);
    var len = t.length >>> 0;

    if (len === 0) {
      return -1;
    }
    var n = 0;
    if (arguments.length > 1) {
      n = Number(arguments[1]);
      if (n != n) { // shortcut for verifying if it's NaN
        n = 0;
      } else if (n != 0 && n != Infinity && n != -Infinity) {
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
      }
    }
    if (n >= len) {
      return -1;
    }
    var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
    for (; k < len; k++) {
      if (k in t && t[k] === searchElement) {
        return k;
      }
    }
    return -1;
  }
}
Run Code Online (Sandbox Code Playgroud)

上面的例子是取自MDN文章Array.prototype.indexOf.