javascript 不支持 IE 8 中的 indexOf?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9768574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:46:36  来源:igfitidea点击:

No support for indexOf in IE 8?

javascriptjqueryhtmlinternet-explorerinternet-explorer-8

提问by AabinGunz

I have a requirement where I create radio buttons dynamically based upon JSON response. So far what I did works in Chrome and firefox, but gives Object doesn't support this property or methodon if(subItem[1].indexOf(",") >= 0)line

我需要根据 JSON 响应动态创建单选按钮。到目前为止,我所做的Chrome和Firefox的作品,但给Object doesn't support this property or methodif(subItem[1].indexOf(",") >= 0)线

My code

我的代码

$("#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/>');
});

Update

更新

Hereis a List of various function code not supported by IE8

是IE8不支持的各种功能代码列表

UpdateWhat is the problem here, for every value it gives me -1I am using code given by Sudhir

更新这里有什么问题,对于它给我的每个值,我-1使用的是 Sudhir 提供的代码

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

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

Screenshot

截屏

enter image description here

在此处输入图片说明

Update

更新

Got it here, var temp=subItem[1].toString();was the problem, converting it to String worked.

到了这里,var temp=subItem[1].toString();是问题所在,将其转换为 String 有效。

回答by Sudhir Bastakoti

IE versions < 9 don't have indexOf, so you can add your own:

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 

回答by Vanji

According to http://msdn.microsoft.com/en-us/library/ie/53xtt423(v=vs.94).aspx

根据http://msdn.microsoft.com/en-us/library/ie/53xtt423(v=vs.94).aspx

MS has said that it support indexof() to IE8 as well!

MS 说它也支持 indexof() 到 IE8!

回答by davidethell

What happens if you do this instead?:

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

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

I know I've had cases where it seemed to not understand what the object type was when reference from an array instead of a variable created from the contents of that array location. It doesn't make sense that this would be the case, but I've used it as a workaround before.

我知道我遇到过这样的情况,当从数组而不是从该数组位置的内容创建的变量进行引用时,它似乎不明白对象类型是什么。这种情况是没有意义的,但我以前曾将其用作解决方法。