0

我正在使用这个 JS 代码来访问城市列表。它适用于烟花,chrome .etc 但在 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>

4

1 回答 1

0

IE7(及之前,以及处于“兼容”模式的 IE8)存在问题getElementById,它会考虑一些不应该使用的 ID。我怀疑你有一个全局变量,或者另一个带有 的元素name="citytd",而 IE 会选择它(尽管它不应该)。更多:......任何其他名字,都会闻起来很甜

于 2013-03-25T09:23:20.310 回答