Why doesn't indexOf work on an array IE8?

后端 未结 7 805
耶瑟儿~
耶瑟儿~ 2020-11-22 04:15

The below function works fine on Opera, Firefox and Chrome. However, in IE8 it fails on the if ( allowed.indexOf(ext[1]) == -1) part.

Does anyone know w

相关标签:
7条回答
  • 2020-11-22 05:21

    You can use this to replace the function if it doesn't exist:

    <script>
    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function(elt /*, from*/) {
            var len = this.length >>> 0;
    
            var from = Number(arguments[1]) || 0;
            from = (from < 0) ? Math.ceil(from) : Math.floor(from);
            if (from < 0)
                from += len;
    
            for (; from < len; from++) {
                if (from in this && this[from] === elt)
                    return from;
            }
            return -1;
        };
    }
    </script>
    
    0 讨论(0)
提交回复
热议问题