javascript - .indexOf() 在 IE8 中不工作

标签 javascript jquery internet-explorer-8

<分区>

我为其中一个工作平台创建了这个小交互 - http://jsfiddle.net/S79qp/427/

它在除 IE8 之外的所有浏览器中都能正常工作。当我运行控制台时,似乎是这部分出现问题...

index = a.indexOf(current) + 1;
if (index < 0 ||  index >= l.length) {
    index = 0;
}

谁能告诉我一个 IE8 友好的替代品,这样我就可以让它与所需的版本兼容?

最佳答案

当您查看 MDN 的 Array.indexOf 时文章,你会看到的

indexOf is a recent addition to the ECMA-262 standard; as such it may not be present in all browsers. You can work around this by utilizing the following code at the beginning of your scripts. This will allow you to use indexOf when there is still no native support. This algorithm matches the one specified in ECMA-262, 5th edition, assuming Object, TypeError, Number, Math.floor, Math.abs, and Math.max have their original values.

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

关于javascript - .indexOf() 在 IE8 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16187428/

相关文章:

javascript - 如何将JavaScript中的字符串转换为字节序列?

javascript - 多人游戏的jQuery替代品?

css - CSS导航菜单下拉背景显示的宽度仅在ie8中大于设置的宽度

javascript - JSP加载耗时超过12秒

javascript - 聊天应用程序不会在 jquery 中每 10 秒加载一次

ruby-on-rails - Rails 3 确认删除 - 在 IE8 中,按取消仍会删除

java - 如何从记事本/电子表格导入数据集到Matlab GUI

javascript - 返回带有元素的Javascript数组,但是当记录长度时,我得到零

javascript - 使用变量的 jQuery 动画在 OSX Safari 中不起作用

jquery - 水平滚动-移动-滑动?