Javascript 我能';你找不到错误吗?TypeError:document.getElementById(…)为空

Javascript 我能';你找不到错误吗?TypeError:document.getElementById(…)为空,javascript,Javascript,使用以下代码,成功显示结果 window.onload = setInterval(func_name, 5000); function func_name() { var ids = document.getElementById('aa').value; ids_array = ids.split(','); for (var i in ids_array) { if (document.getElementById('a' + ids_array[

使用以下代码,成功显示结果

window.onload = setInterval(func_name, 5000);

function func_name() {
    var ids = document.getElementById('aa').value;
    ids_array = ids.split(',');
    for (var i in ids_array) {
        if (document.getElementById('a' + ids_array[i])) {
            document.getElementById('a' + ids_array[i]).innerHTML = ids_array[i];
但是,当我使用AJAX请求时,我得到了错误:
TypeError:document.getElementById(…)为null

var xmlhttp;
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById('a' + ids_array[i]).innerHTML = xmlhttp.response; // error is here... TypeError: document.getElementById(...) is null
    }
}
xmlhttp.open("GET", "<?php echo baseurl . 'notification.php';?>?users_id=" + ids_array[i], true);
xmlhttp.send();
}
}
}
}
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById('a'+ids_array[i]).innerHTML=xmlhttp.response;//此处有错误…类型错误:document.getElementById(…)为空
}
}
open(“GET”,“?users_id=“+ids_array[i],true”);
xmlhttp.send();
}
}
}
}

我是初学者,很抱歉出现这种类型的代码

您没有发布任何有关浏览器报告问题的详细信息(代码行),但我的猜测是:

var ids = document.getElementById('aa').value;
最可能的罪魁祸首是您没有id(
id=“aa”
)为
aa
的元素。要么就是你做了这样奇怪的事:

xmlhttp.onreadystatechange = (function(i) {
    return function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('a' + ids_array[i]).innerHTML = xmlhttp.response; // error is here... TypeError: document.getElementById(...) is null
        }
    }
}(i));
function func_name() {
    var ids = document.getElementById('aa').value;
    ids_array = ids.split(',');
    for(var i=0; i<ids_array.length; i++) {
        if (document.getElementById('a' + ids_array[i])) {
            (function(i) {
                callAjax("<?php echo baseurl . 'notification.php';?>?users_id=" + ids_array[i], function(response){
                    document.getElementById('a' + ids_array[i]).innerHTML = response;
                });
            }(i));
        }
    }
}

function callAjax(url, callback) {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
document=…


在代码的某个地方。它是
null
而不是
undefined
有点奇怪。

您的ids\u数组[i]变量在xmlhttp.onreadystatechange=function()中没有正确定义, 因为“i”变量在每个FOR循环迭代中都会重新定义

因此,所有代码都应该是:

var ids = document.getElementById('aa').value;
var ids_array = ids.split(',');

for (var i=0; i<ids_array.length; i++)
{
  if (document.getElementById('a' + ids_array[i])) {
    // For every iteration, create a closure that 
    // stores the "i" variable multiple times with different values in the closure.
    // Also create an xmlhttp Object for each request.
    var closure = function()
    {
      var xmlhttp;
      if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
      } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById('a' + ids_array[i]).innerHTML = result;
          // After this function executes, it and any closures it has will be
          // available for garbage collection.
        }
      }
      xmlhttp.open("GET", "<?php echo baseurl . 'notification.php';?>?users_id=" + ids_array[i], true);
      xmlhttp.send();

      // This code ends, but xmlhttp objects wait for onreadystatechange event.
    }
  }
}
window.onload = function() {
    setInterval(func_name, 5000);
}
var id=document.getElementById('aa').value;
var ids_array=ids.split(',');

对于(var i=0;i您的代码有几个问题。首先,在末尾有一个额外的
}
。另外,
window.onload=setInterval(func_name,5000);
应该是:

var ids = document.getElementById('aa').value;
var ids_array = ids.split(',');

for (var i=0; i<ids_array.length; i++)
{
  if (document.getElementById('a' + ids_array[i])) {
    // For every iteration, create a closure that 
    // stores the "i" variable multiple times with different values in the closure.
    // Also create an xmlhttp Object for each request.
    var closure = function()
    {
      var xmlhttp;
      if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
      } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById('a' + ids_array[i]).innerHTML = result;
          // After this function executes, it and any closures it has will be
          // available for garbage collection.
        }
      }
      xmlhttp.open("GET", "<?php echo baseurl . 'notification.php';?>?users_id=" + ids_array[i], true);
      xmlhttp.send();

      // This code ends, but xmlhttp objects wait for onreadystatechange event.
    }
  }
}
window.onload = function() {
    setInterval(func_name, 5000);
}
然后,
for(ids\u数组中的var i)
应该是

for(var i=0; i<ids_array.length; i++) { ...
有关原因的更多解释,请访问


我意识到还有一件事:要触发多个这样的请求,需要多个XMLHttpRequest对象。我建议使用单独的函数启动ajax请求,如下所示:

xmlhttp.onreadystatechange = (function(i) {
    return function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('a' + ids_array[i]).innerHTML = xmlhttp.response; // error is here... TypeError: document.getElementById(...) is null
        }
    }
}(i));
function func_name() {
    var ids = document.getElementById('aa').value;
    ids_array = ids.split(',');
    for(var i=0; i<ids_array.length; i++) {
        if (document.getElementById('a' + ids_array[i])) {
            (function(i) {
                callAjax("<?php echo baseurl . 'notification.php';?>?users_id=" + ids_array[i], function(response){
                    document.getElementById('a' + ids_array[i]).innerHTML = response;
                });
            }(i));
        }
    }
}

function callAjax(url, callback) {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
函数函数名(){
var id=document.getElementById('aa')。值;
ids_array=ids.split(',');

对于(var i=0;提到iOP,第一个代码块是可以的,但是第二个代码块出现在
document.getElementById('a'+ids\u array[i])行上的错误.innerHTML=xmlhttp.response;
回调中的
i
将始终是
ids\u array.length+1
。您需要包括另一个作用域;例如在我删除的中断中,在代码之后,我添加这个xmlhttp.open(“GET”?“users\u id=“+ids\u array[i],true”);xmlhttp.send();它是否正确?@Matt for(ids\u array中的变量i)迭代ids_数组中的所有元素(即0,1,2,3),但它当然会迭代“ids_array.length”元素,因此它看起来像0,1,2,3,length您不能将循环移动到回调,
i
也用于在
xmlhttp.open()中构造URL
@Nikolay Gromov ReferenceError:我没有定义。可能我应该在循环中插入最后两行,但我不确定..欢迎使用堆栈溢出,无论您是否开始编码,请花更多时间格式化您的代码,并在下次正确提问。这一次,社区中的一些成员已经介入以清除它或者你。是的,我试过你的答案,但我总是在php文件中收到数组的最后一个值…我相信现在我看到了另一个问题:你不能用代码在一个循环中触发多个请求,你需要多个XMLHttpRequest实例。这让我想到:你不能摆脱整个循环并触发一个请求,一次传递所有ID,然后与您需要的所有信息相对应?我有一个表,第一列应该是关于新消息的通知(消息),第二个是用户的全名。因此,我检查数组的元素,如果用户有新消息,则会显示通知。@vazgen好的,我建议使用另一种方法,但无需担心。请参阅我的更新,我希望我刚才添加的代码对您有用。