Javascript:未捕获的类型错误:无法设置 null 的属性“innerHTML”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20659932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:59:02  来源:igfitidea点击:

Javascript: Uncaught TypeError: Cannot set property 'innerHTML' of null

javascripthtml

提问by Tan WS

I've browsed through a couple of questions but I couldn't find any help. By the way this is the first time I'm asking a question here so I might not do it the right way. Anyway, the following line is causing me the problem

我浏览了几个问题,但找不到任何帮助。顺便说一下,这是我第一次在这里提问,所以我可能不会以正确的方式提问。无论如何,以下行导致了我的问题

/* cursorPps defined above */
document.getElementById("cursorPps").innerHTML = cursorPps;

The cursorPpsvariable is already defined. Can someone point out what other possible stuff could have caused this error?

cursorPps变量已定义。有人可以指出其他可能导致此错误的原因吗?

Edit: By the way the problem is that it is not updating the value on HTML, although the value of the variable changes.

编辑:顺便说一下,问题是它没有更新 HTML 上的值,尽管变量的值发生了变化。

HTML:

HTML:

<html>

<head>
<title>Potato Clicker</title>
<link rel="stylesheet" type="text/css" href="interface.css">
</head>

<body>

<div id="left">
<img id="potato-img" onClick="potatoClick(clickPower)" src="stockvault-potatoes107220.jpg" width="300" height="300">
<br>
<div id="mainDisplay">
<span id="potatoes">0</span> <br> potatoes
<br>
<br>
Producing <span id="pps">0</span> potatoes per second
<br>
</div>
Cursors: <span id="cursors">0</span>
</div>

<div id="middle">
<div id="buildings" onClick="buyCursor()"> &nbsp; Buy Cursor &nbsp; </div>
</div>


<script src="main.js"></script>
</body>

</html>

Javascript:

Javascript:

//variables

var potatoes = 0;   //potatoes
var clickPower = 1; //potatoes gained per click
var pps = 0;        //potatoes per second
var cursors = 0;    //cursors
var cursorCost;     //cost of cursor
var cursorPps = 0;  //total cursor potatoes per second
var cursorBuy;      //cursor buy button


//functions

function potatoClick(number) {

potatoes = potatoes + number;
document.getElementById("potatoes").innerHTML = potatoes;

}

function buyCursor() {

var cursorCost = Math.floor(10 * Math.pow(1.2,cursors));
if (potatoes >= cursorCost) {
pps = pps - cursorPps;
cursors = cursors + 1;
potatoes = potatoes - cursorCost;
cursorPps = cursorPps + 1;
pps = pps + cursorPps;
document.getElementById("potatoes").innerHTML = potatoes;
document.getElementById("cursors").innerHTML = cursors;
document.getElementById("cursorPps").innerHTML = cursorPps;
document.getElementById("pps").innerHTML = pps;
}
else {
alert("Not enough potatoes!")
}
var nextCost = Math.floor(100 * Math.pow(1.2,cursors));       
document.getElementById('cursorCost').innerHTML = nextCost; 
}

window.setInterval(function () {

if (pps > 0) {

potatoClick(pps);

}

}, 1000);

回答by leopic

Make sure that

确保

  • You have an element has that ID
  • The element IS on the page before that Javascript is run
  • You bind the execution of your JS to an event (follow-up from previous) that happens after the element is on the page
  • The cursorPps variable has been populated
  • 您有一个元素具有该 ID
  • 在运行 Javascript 之前页面上的元素是
  • 您将 JS 的执行绑定到在元素位于页面上之后发生的事件(上一个的后续)
  • 已填充 cursorPps 变量

Here's a simple codepen that demonstrates a way to do it http://codepen.io/leopic/pen/wDtIB

这是一个简单的 codepen,演示了一种方法http://codepen.io/leopic/pen/wDtIB

回答by GLES



<div id="myDiv"></div>


  • You have an element with that ID

    var length = document.querySelectorAll('#myDiv').length;
    if(length > 0) {
        // This element exists in the DOM tree
    }
    
  • You bind the execution of your JS to an event (follow-up from previous) that happens after the element is on the page

    <body>
        <div id="myDiv"></div>
        <script>
            var length = document.querySelectorAll('#myDiv').length;
            console.log(length);
        </script>
    </body>
    
  • The cursorPps variable has been populated

    if(cursorPps != undefined)
    
  • 您有一个具有该 ID 的元素

    var length = document.querySelectorAll('#myDiv').length;
    if(length > 0) {
        // This element exists in the DOM tree
    }
    
  • 您将 JS 的执行绑定到在元素位于页面上之后发生的事件(上一个的后续)

    <body>
        <div id="myDiv"></div>
        <script>
            var length = document.querySelectorAll('#myDiv').length;
            console.log(length);
        </script>
    </body>
    
  • 已填充 cursorPps 变量

    if(cursorPps != undefined)