.indexOf使用JavaScript在IE7 / 8上运行的数组上的函数 [英] .indexOf function on an array not working in IE7/8 using JavaScript

查看:263
本文介绍了.indexOf使用JavaScript在IE7 / 8上运行的数组上的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我IE 7和IE 8是否支持JavaScript .indexOf()方法,因为我收到错误:

Can anyone tell me if IE 7 and IE 8 support the JavaScript .indexOf() method as I am receiving the error:

SCRIPT438: Object doesn't support property or method 'indexOf' 

在IE7和IE8浏览器模式下使用。)

from the IE9 debug console (used under both IE7 and IE8 Browser mode).

对于以下注释,使用.indexOf()的代码如下:

For the below comment, the code using .indexOf() is as follows:

if(shirt_colour == 'black') {
    p_arr=['orange','red','green','yellow','bblue','rblue','pink','white','silver','gold'];
    if( p_arr.indexOf(print_colour) != -1 ) rtn = true;
}


推荐答案

在IE< 9 indexOf()它没有很好实现。尝试在代码上添加此函数:

On IE<9 indexOf() it is not "well" implemented. Try to add this function on your code :

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

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

如果在ECMA中找不到,它将替换原来的功能 - 262标准。

It will "replace" the original function, if not found in the ECMA-262 standard.

这篇关于.indexOf使用JavaScript在IE7 / 8上运行的数组上的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆