转自:http://www.jbxue.com/article/8367.html

原因分析:

这是一个js bug, 在IE8下,js数组没有indexOf方法,会报错;而在其它浏览器下(Firefox, Chrome, IE9)都是正常的。

解决方案:

1.个人在网上找到了一种解决方法:
就是在使用indexOf方法前,验证一下是否存在该方法IndexOf方法,如果有调用;如果没有就添加一个:
代码如下:

//添加数组IndexOf方法
if (!Array.prototype.indexOf){
Array.prototype.indexOf = function(elt /*, from*/){
var len = this.length >>> ; var from = Number(arguments[]) || ;
from = (from < )
? Math.ceil(from)
: Math.floor(from);
if (from < )
from += len; for (; from < len; from++){
if (from in this && this[from] === elt)
return from;
}
return -;
};
}

2.对于本ID来说,我已经引入了jquery的库函数,发现jquery里面也有类似工具函数(jQuery.inArray()方法):

就使用jQuery.inArray()替换了Array.prototype.indexOf,使用实例如下:

var arr = [ , "Pete", , "John" ];
jQuery.inArray("John", arr); //
jQuery.inArray(, arr); //
jQuery.inArray("David", arr); //-1
jQuery.inArray("Pete", arr, ); //-1

3. 上面的方法也可以结合使用。

IE8不支持数组的indexOf方法 如何解决的更多相关文章

  1. 终于解决了IE8不支持数组的indexOf方法,array的IndexOf方法

    /* 终于解决了IE8不支持数组的indexOf方法 */ if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (el ...

  2. IE8不支持数组的indexOf方法

    在IE8下有个js错误,但是在其它浏览器下(Firefox, Chrome, IE9)下面都很正常.后来调试发现原因是在IE8下,js数组没有indexOf方法. 在使用indexOf方法前,执行一下 ...

  3. 解决了IE8不支持数组的indexOf方法

    ie在过去给我们添了很多坑. if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/ ) { ...

  4. js解决IE不支持数组的indexOf()方法

    if (!Array.indexOf) {                                    Array.indexOf = function (obj) {            ...

  5. JavaScript——数组的indexOf()方法在IE8中的兼容性问题

    昨天在工作中遇到一个问题:数组的indexOf()方法在IE8中无效. 如以下代码在IE8中报错“对象不支持“indexOf”属性或方法”: var arr = [1,2,3]; var index ...

  6. JavaScript—从数组的indexOf方法深入——Object的Property机制。

    在js中,可以说万物皆对象(object),一个数组也是一个对象(array). 很多对象都有很多很方便的方法 比如数组的push,concat,slice等等,但是如果一些对象,它没有实现这些方法, ...

  7. js 判断数组包含某值的方法 和 javascript数组扩展indexOf()方法

    var  questionId = []; var anSwerIdValue = []; ////javascript数组扩展indexOf()方法 Array.prototype.indexOf ...

  8. 数组的indexOf方法--数组去重

    数组的indexOf方法 数组方法大家再熟悉不过了,却忽略了数组有 indexOf 这个方法(我个人感觉). 干说不练瞎扯淡,遇到了什么问题,注意⚠️点又在哪里? let arr = ['orange ...

  9. IE8数组不支持indexOf方法的解决办法

    在使用indexof方法之前加上以下代码就可以了. if (!Array.prototype.indexOf){           Array.prototype.indexOf = functio ...

随机推荐

  1. java的缓存框架

    1.java里面有一些开源的缓存框架,比如ecache,memcache,redis等缓存框架. 2.使用缓存框架的原理就是减少数据库端的压力,将缓存数据放在内存里面,存储成键值对的格式,这样可以不去 ...

  2. django的forms

    base知识 参考博文:https://www.cnblogs.com/yuanchenqi/articles/9036474.html 用户表单是Web端的一项基本功能,大而全的Django框架中自 ...

  3. express无中间件的增删改查

    index.js const express = require("express");导入express框架 const data = require("./data& ...

  4. Ubuntu 提权漏洞(CVE-2019-7304)复现

    漏洞描述: Ubuntu 版本: Ubuntu 18.10 Ubuntu 18.04 LTS Ubuntu 16.04 LTS Ubuntu 14.04 LTS 2.28 < snapd < ...

  5. JQ 确定与取消弹出框,选择确定执行Ajax

    $(function () { $("#GetCoupon").click(function () { function del() { var msg = "请确定领取 ...

  6. Apache Maven 入门篇

    2017-11-09注释:IntelliJ IDEA 2017.2.5 x64 等新版本会安装maven,为了有好的体验 建议在安装目录找到IntelliJ IDEA 2017.2.5\plugins ...

  7. 7. myeclipse10反编译插件安装

  8. The 2018 Nobel prizesThe Nobel prize for economics is awarded for work on the climate and economic growth

    The 2018 Nobel prizesThe Nobel prize for economics is awarded for work on the climate and economic g ...

  9. array numpy 模块

    高级用法:http://www.jb51.net/article/87987.htm from array import * 调用 array 与 import numpy as np  调用 np. ...

  10. Windows驱动手动卸载与安装

    彻底卸载的流程 1.删除C:\windows\inf\oem.inf路径下的所有oem文件 2.删除c:\windows\system32\drivers路径下对应的sys文件 3.(重要) 第一步: ...