什么原因导致IE7错误?

时间:2013-03-25 09:18:49

标签: javascript ajax

我正在使用此JS代码来访问城市列表。它适用于烟火,铬.e.t.c。但在ie7中却没有。第document.getElementById(oDiv).innerHTML=xmlhttp.responseText;行会导致错误。

当我将“responseText”更改为“readyState”,“statusText”,“readyState”时,脚本可以正常工作。只有“responseText”会导致问题。

这里有什么问题?

function showAjax(oDiv, countrycode, dowhat) {
    if (oDiv == "") {
        document.getElementById(oDiv).innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open('POST', 'ajax.php?dowhat=' + dowhat + '&countrycode=' + countrycode, true);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(oDiv).innerHTML = xmlhttp.responseText;
            //document.getElementById(oDiv).innerHTML=xmlhttp.readyState;
        }
    }
    xmlhttp.send();
}

<a href='#' onclick="showAjax('citytd','$countrycode','listcities')">Click</a> <div id=citytd></div>

1 个答案:

答案 0 :(得分:0)

IE7(之前和IE8中的#34;兼容性&#34;模式)与getElementById存在问题,它会考虑一些不应该使用的ID。我怀疑你有一个全局变量,或者另一个带有name="citytd"的元素,而IE则会选择它(尽管它也不应该这样)。更多:...by any other name, would smell as sweet