简体   繁体   English

获取“无法读取null的属性'innerHTML'”,但我启动了该ID

[英]Getting “Cannot read property 'innerHTML' of null” but I have that id initiated

I have read on here that usually this problem comes up when the specific div isn't loaded yet or just isn't there. 我在这里读到,通常在未加载特定div或不存在特定div时会出现此问题。 I have this code looping so even if it wasn't fully loaded the first time it will be by the next iteration. 我有这段代码循环,所以即使第一次没有完全加载它,也要在下一次迭代中进行。 Also I for sure have a div with the id participants. 我当然也有一个ID参与者的div。

    <div id="participants">
         <div class="sectionhead wow bounceInUp" data-wow-duration="2s">
            <span class="bigicon icon-user"></span>
            <h3>Participants<h3>
            <h4>Check out who has signed up already!</h4>
            <hr class="separetor">

            <div id"test" onload="updateVariables()">
            </div>
         </div>
    </div>


<script>
setInterval(function(){
    updateVariables();
},4000);
updateVariables = function() {
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var info = xmlhttp.responseText;
        console.log(info);
        var out = info.split("\n");
        console.log(out[1]);
        var outLen = out.length;
        for(x = 0; x < outLen; x++){
          document.getElementById("test").innerHTML += out[x];
          document.getElementById("test").innerHTML += "<br>";
        }
    }
  }
xmlhttp.open("GET","../php/tour.txt",true);
xmlhttp.send();
}
</script> 

That's pretty simple. 那很简单。 In your real page you have a simple typo mistake. 在您的真实页面中,您有一个简单的错字错误。 Instead of 代替

<div id"test" onload="updateVariables()">

You should have 你应该有

<div id="test" onload="updateVariables()">

EDIT: the easiest way to locate such errors on your own is debugging. 编辑:自行查找此类错误的最简单方法是调试。 I consider at the moment that Chrome dev tools is the best option, however you can use dev tools F12 of any browser, since this problem is simple. 目前,我认为Chrome开发人员工具是最好的选择,但是您可以使用任何浏览器的开发人员工具F12 ,因为此问题很简单。 I will demonstrate an example on Chrome. 我将在Chrome上演示一个示例。

In console you will see errors happening. 在控制台中,您将看到发生的错误。 To the right of the error you can see a link to the place in sources where it happens. 在错误的右侧,您可以看到在发生错误的源中到该位置的链接。 在此处输入图片说明 A click on (index):247 takes you there in debugger window. 单击(索引):247,将您带到调试器窗口。 Where you can place a breakpoint. 您可以在其中放置断点的位置。 Once you hit the breakpoint you have very powerful tools provided by Chrome. 达到断点后,Chrome将提供非常强大的工具。 You can add variables to watch list, you can execute any code in console, you can trace the DOM (Elements tab) at current moment. 您可以将变量添加到监视列表,可以在控制台中执行任何代码,还可以在当前时刻跟踪DOM(“元素”选项卡)。 在此处输入图片说明 The typo of interest can be easily located by copying the code you suppose is working to console document.getElementById("test") . 可以通过复制您假设正在控制台document.getElementById("test")的代码轻松找到感兴趣的错字。

Now you start getting puzzled what the heck it returns null instead of a div. 现在,您开始感到困惑,到底返回null而不是div的原因是什么。 You go to elements tab and search for test by Ctrl+F . 您转到元素选项卡,然后按Ctrl + F搜索test You find the text in html however after roaming around with beer you notice that the id is actually not defined due to wrong syntax 您在html中找到了文本,但是在与啤酒漫游之后,您会发现由于语法错误而实际上未定义id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM