简体   繁体   English

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

[英]jQuery - Object doesn't support this property or method in IE8

I have implemented sticky notes functionality in one of my projects and I have found a problem related to browser compatibility. 我在我的一个项目中实现了便签功能,我发现了与浏览器兼容性相关的问题。 My code is not working in IE8, but it works fine in other browsers. 我的代码在IE8中不起作用,但在其他浏览器中工作正常。

The error I get is: 我得到的错误是:

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

These are the lines between no 40 to 50: 这些是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);
    });

Any help on what the problem might be would be much appreciated. 任何有关问题的帮助都将非常感激。

IE8 does not support .indexOf() . IE8不支持.indexOf() In Internet Explorer it was first introduced with IE9. 在Internet Explorer中,它首次引入了IE9。

Since you are using jQuery, you can use the $.inArray() method instead. 由于您使用的是jQuery,因此可以使用$.inArray()方法。

Something like this, in your case: 在你的情况下,像这样的东西:

if($.inArray(note.id, lastCreatedNoteIdForJs) != -1)

Support for older browsers without jQuery: 支持没有jQuery的旧版浏览器:

If you want to use .indexOf() in legacy browsers as well, you use your own array-prototype when needed. 如果您想在旧版浏览器中使用.indexOf() ,则需要时使用自己的数组原型。 The prototype would look something like this: 原型看起来像这样:

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;
  }
}

Above example is taken from the MDN article on Array.prototype.indexOf . 以上示例摘自Array.prototype.indexOf上的MDN文章

Check this link it may help you... IndexOf wil work only in certain browsers .. 检查此链接它可能会帮助您... IndexOf仅在某些浏览器中工作..

How to fix Array indexOf() in JavaScript for Internet Explorer browsers 如何在JavaScript中为Internet Explorer浏览器修复Array indexOf()

IE < 9 doesn't support the .indexOf method for arrays. IE <9不支持数组的.indexOf方法。

jQuery has a method which allows you to check if the element is in the array. jQuery有一个方法,允许您检查元素是否在数组中。

if($.inArray(note.id, lastCreatedNoteIdForJs) !== -1) {
    passId = lastCreatedNoteId[note.id];   
}

You could also add the method to the Array object: How to fix Array indexOf() in JavaScript for Internet Explorer browsers 您还可以将该方法添加到Array对象: 如何在JavaScript中为Internet Explorer浏览器修复Array indexOf()

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(obj, start) {
        for (var i = (start || 0), j = this.length; i < j; i++) {
            if (this[i] === obj) { return i; }
        }
        return -1;
    }
}

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

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