简体   繁体   English

无法在附加子项(div)上将属性innerHTML设置为null

[英]Cannot set property innerHTML of null on appended child (div)

Before I learn some Unity I wanted to be able to create a simple Javascript game out of my own knowledge. 在学习Unity之前,我希望能够根据自己的知识创建一个简单的Javascript游戏。 So, I started experimenting with a timer and it worked. 因此,我开始尝试使用计时器,它开始起作用。 Then I wanted to add a div element to my HTML document and have its text change in response to the timer. 然后,我想向我的HTML文档中添加一个div元素,并更改其文本以响应计时器。 So I wrote this code: 所以我写了这段代码:

 var counter = 0; var seconds = 0; $(document).ready(function(e) { var element = document.createElement("div"); element.id = "text2"; document.getElementById("canvas").appendChild(element); function gameActions() { document.getElementById("canvas").innerHTML = seconds; if (seconds == 1) document.getElementById("text2").innerHTML = "second"; else document.getElementById("text2").innerHTML = "seconds"; counter++; if (counter % 50 == 0) { seconds++; } } setInterval(gameActions, 20); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="canvas" class="imgcenter" style="width: 500px; height: 400px; background-color: #eeffee;"> </div> 

Basically I'm intending to change the text of the "parent" div which displays the number of seconds passed after the html page is fully loaded. 基本上,我打算更改“ parent” div的文本,该文本显示html页面完全加载后经过的秒数。 But before that, I'm trying to create a div with id text2 to add it inside the div parent.This div should display the word "seconds" depending on the time (basically display "second" when the seconds variable is 1, and "seconds" when not) 但在此之前,我试图创建一个div ID为text2添加的div parent.This DIV中应显示“秒”字取决于时间(基本上显示为“第二”,当秒变量为1,如果不是,则为“秒”)

Where "imgcenter" is only CSS to specify auto margin. 其中“ imgcenter”仅是用于指定自动页边距的CSS。 The error is that, whenever I run this on Chrome, I get: 错误是,每当我在Chrome上运行此命令时,都会得到:

Uncaught TypeError: Cannot set property 'innerHTML' of null

Am I correct to assume the div is not being added? 我是否正确假设未添加div? Am I missing a step in the addition? 我在添加中缺少步骤了吗? Please let me know what you think the error is, thank you 请让我知道您认为错误是什么,谢谢

This code: 这段代码:

var element = document.createElement("div");
element.id = "text2";
document.getElementById("canvas").appendChild(element);

… puts the element you are trying to target inside the canvas div. …将您要定位的元素放在canvas div中。

This code: 这段代码:

    document.getElementById("canvas").innerHTML = seconds;

destroys everything in that element and replaces it with the HTML in the seconds variable. 销毁该元素中的所有内容,并用seconds变量中的HTML替换它。

Am I correct to assume the div is not being added? 我是否正确假设未添加div?

No. The div is added, but then you destroy it before trying to do anything with it. 不会。div已添加,但是您在尝试对其进行任何操作之前先将其销毁。

In your line document.getElementById("canvas").innerHTML = seconds; 在您的行document.getElementById("canvas").innerHTML = seconds; you are replacing your complete innterHTML of canvas . 您将替换canvas的完整intterHTML。 By that your element text2 is removed again. 这样,您的元素text2再次被删除。

If you want to show the unit independently from the seconds in it's own element, you should create an element unit for example like this: 如果要独立于秒在其自己的元素中显示单位,则应创建一个像这样的元素unit

 var counter = 0; var seconds = 0; $(document).ready(function (e) { var element = document.createElement("div"); var unit = document.createElement("div"); // create a second element unit element.id = "text2"; unit.id = "unit"; // asign the id unit document.getElementById("canvas").appendChild(element); document.getElementById("canvas").appendChild(unit); function gameActions() { document.getElementById("unit").innerHTML = seconds; // replace the unit with the valuen of seconds if (seconds == 1) document.getElementById("text2").innerHTML = "second"; else document.getElementById("text2").innerHTML = "seconds"; counter++; if(counter % 50 == 0) { seconds++; } } setInterval(gameActions, 20); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="canvas" class="imgcenter" style="width: 500px; height: 400px; background-color: #eeffee;"> </div> 

You are removing the element you added when you replace the inner HTML of the canvas. 您将删除替换画布内部HTML时添加的元素。 It would be better if you just updated the entire contents with the entire string instead of creating a separate element. 如果只用整个字符串更新整个内容,而不是创建一个单独的元素,那会更好。 I would also wrap it in an IIFE so that the variables don't leak into the global scope. 我还将其包装在IIFE中,以使变量不会泄漏到全局范围中。

(function () {
    var counter = 0;
    var seconds = 0;

    var canvas = document.getElementById("canvas");

    function gameActions() {
        canvas.innerHTML = seconds + " second" + (seconds > 1 ? "s" : "");

        counter++;

        if (counter % 50 == 0) {
            seconds++;
        }

    }

    $(function() {
        setInterval(gameActions, 20);
    });
})();

http://jsfiddle.net/1unn153b/ http://jsfiddle.net/1unn153b/

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

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