IE 8不支持indexOf吗?

5

我有一个需求,根据JSON响应动态创建单选按钮。到目前为止,我所做的在Chrome和Firefox中有效,但在if(subItem[1].indexOf(",") >= 0)行上给出了对象不支持此属性或方法的错误。

我的代码如下:

$("#sList").live("change", function(){
    var currentService=this.value;
    var c1Svc=[];
    var c2Svc=[];

    if(absP.length==2)
    {
        $.each(compareServiceData,function(i,item){

            if(currentService==item[0])
            {
                var configCount=0;
                $.each(item[1],function(j,subItem){

                    var temp=subItem[1];

                    /*The JSON response contains List of Lists, so here if it contains a list it will be separated by "," so split it and store in a array, else directly store in a array*/
                    if(subItem[1].indexOf(",") >= 0)
                    {
                        var tList=temp.split(",");
                        $.each(tList,function(k,val){
                            if(configCount==0)
                            {
                                c1Svc.push(val);                                
                            }
                            else
                            {
                                c2Svc.push(val);                                
                            }
                        });
                    }
                    else
                    {
                        if(configCount==0)
                        {
                            c1Svc.push(subItem[1]);                             
                        }
                        else
                        {
                            c2Svc.push(subItem[1]);                             
                        }
                    }
                    configCount++;

                });
            }

        });

        if ($("#customServiceListing").length == 0)
        {               
            $("#compareContent").append('<table id="customServiceListing" align="center" width="90%" class="csm-table" border="1"><tbody><tr><td><form id="c1Service"></form></td><td><form id="c2Service"></form></td></tr></tbody></table>');
        }
        else
        {
            $('#c1Service').empty();
            $('#c2Service').empty();
        }

    }
    else
    {
        $("#compareContent").append('<table align="center" width="90%" class="csm-table" border="1"><tbody><tr><td><form><select id="c1Service"></select></form></td><td><select id="c2Service"></select></td><td><select id="c3Service"></select></td></tr></tbody></table>');
    }


    /*adding service radios to config1*/
    $.each(c1Svc,function(i,item){
        $("#c1Service").append('<input type="radio" name="customConfig1ServiceNames" id="'+item+'" value="'+i+1+'"/>'+item);
    });
    if(c1Svc.length>1)
        $("#c1Service").append('<br/>');

    /*adding service radios to config2*/
    $.each(c2Svc,function(i,item){
        $("#c2Service").append('<input type="radio" name="customConfig2ServiceNames" id="'+item+'" value="'+i+1+'"/>'+item);
    });
    if(c2Svc.length>1)
        $("#c2Service").append('<br/>');
});

更新

这里是IE8不支持的各种函数代码列表。

更新 问题出在哪里,对于每个值它都给我一个-1。我正在使用Sudhir提供的代码。

alert(subItem[1].indexOf(",")+", "+subItem[1]);

截图

enter image description here

更新

在这里找到了解决方法,var temp=subItem[1].toString(); 是问题所在,将其转换为字符串即可解决。


1
是的,一定要喜欢IE。请看这里:https://dev59.com/MnI-5IYBdhLWcg3wpqMK - McGarnagle
是的,一定要喜欢IE。请看这里:https://dev59.com/MnI-5IYBdhLWcg3wpqMK - McGarnagle
1
IE总是比其他浏览器落后几年。今天早上我已经看到了第二个类似的问题,询问在IE中无法正常工作但在其他地方可以工作的事情。 - Rob
3个回答

16

IE 9以下版本不支持indexOf,因此您可以自行添加:

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

var subItem = [];
subItem[1]="CSMTestPWXListinerService,CSMTestPWXListinerService_ManualyAdded";
console.log(subItem[1].indexOf(","));
//returns 25 

@Rob 好的,我误以为它是转义字符,因为整个代码都在<pre>标签中并进行了转义^^ - Leo
@Sudhir:我从这里使用了indexOf代码,但是当我写alert(subItem[1].indexOf(","));时,其中subItem[1]=CSMTestPWXListinerService,CSMTestPWXListinerService_ManualyAdded它总是返回-1。出了什么问题? - AabinGunz
更新,我现在正在使用您的代码,但是每个值仍然返回-1。 - AabinGunz
@Abhishek Simon:在IE6、7和8中进行了测试,它们似乎都能很好地运行代码。 - Sudhir Bastakoti
请查看我的更新以了解问题所在。谢谢,Sudhir。 - AabinGunz
显示剩余4条评论

1

你正在错误的地方寻找。【不支持】(http://msdn.microsoft.com/zh-cn/library/ie/ff679977(v=vs.94).aspx) - Bergi
1
微软支持字符串的indexOf方法,但不支持数组的indexOf方法!是这样吗? - Vanji

0

如果你这样做会发生什么?

if(temp.indexOf(",") >= 0)

我知道有些情况下,当从数组引用而不是从该数组位置的内容创建的变量时,它似乎不理解对象类型是什么。这看起来不合理,但我以前用过它作为一种解决方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接