elementID.style.display ='块'不是在chrome中工作但在firefox中工作

时间:2014-12-07 21:17:57

标签: javascript google-chrome

function loadMyitem(output, counter, msg, FileQuery) {

    //alert(output)
    if( window.XMLHttpRequest )
    {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 1) {
            msg.style.display = 'block';
            msg.innerHTML = "";
        } else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            msg.style.display = 'block';
            msg.innerHTML = "";

            var respStr = new Array();

            respStr = xmlhttp.responseText.split('|');
            optStr = output.split('|');
            var CC1 = $(counter).val();
            for (i = 0; i < respStr.length; i++) {
                //alert(respStr[i]); 
                document.getElementById(optStr[i] + CC1).value = respStr[i];
            }
        }
    }

    //document.getElementByID(msg).innerHTML=inputText;
    xmlhttp.open("get", FileQuery, true);
    xmlhttp.send();
}

这适用于Firefox,但是当我在chrome或opera中执行此函数时,它会给出错误:

  

未捕获的TypeError:无法设置属性&#39;显示&#39;未定义的

这个功能有什么问题?

1 个答案:

答案 0 :(得分:0)

假设msg是一个带有元素ID的字符串,请尝试此...

var msgElement = document.getElementById(msg);
if (msgElement) {
    msgElement.style.display = 'block';
    // and so on
}