【问题标题】:No support for indexOf in IE 8? [duplicate]IE 8 不支持 indexOf? [复制]
【发布时间】:2012-04-03 20:15:35
【问题描述】:

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

我的代码

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

更新

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

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

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

截图

更新

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

【问题讨论】:

标签: javascript jquery html internet-explorer internet-explorer-8


【解决方案1】:

IE 版本 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 

【讨论】:

  • @Mef 应该是&gt;&gt;&gt;。按位 [零填充右移](developer.mozilla.org/en/JavaScript/Reference/Operators/… 位运算符确保 length 值是一个整数和正数。
  • @Rob 好的,我把它误认为是转义了,因为整个代码是&lt;pre&gt;'d 并转义了^^
  • @Sudhir:我使用了来自here 的indexOf 代码,但是当我写alert(subItem[1].indexOf(",")); 时,subItem[1]=CSMTestPWXListinerService,CSMTestPWXListinerService_ManualyAdded" it returns -1` 即总是返回-1。怎么了?
  • 它对我来说工作正常.. 请参阅答案中添加的代码
  • 更新,我现在正在使用你的代码,但每个值我仍然得到-1
【解决方案2】:

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

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

【讨论】:

  • 你找错地方了。 It's not supported.
  • 所以 MS 支持字符串的 indexOf 但不支持数组!不是吗?
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    相关资源
    最近更新 更多