文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>在IE使用javascript 数组的indexOf

在IE使用javascript 数组的indexOf

时间:2010-06-17  来源:humanux

最近在使用javascript数组时,为了获取数组中元素的索引,使用了indexOf.在firefox中调试ok,但是放到ie中,总是出错,无法执行。查查资料,发现原来在ie中不支持indexOf。如果要使用,需要插入下面的一段代码:

if(!Array.indexOf)

    Array.prototype.indexOf = function(obj)
    {                
        for(var i=0; i<this.length; i++)
        {
            if(this[i]==obj)
            {
                return i;
            }
        }
        return -1;
    }
}
例如:
var   a=new   Array( 'morning', 'lunch', 'afternoon', 'night');
var flag_inout = 'lunch'
//for ie can't support indexof
if(!Array.indexOf){
            Array.prototype.indexOf = function(obj){
                for(var i=0; i<this.length; i++){
                    if(this[i]==obj){
                        return i;
                    }
                }
                return -1;
            }
        }

 var index_id = a.indexOf(flag_inout);

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载