Uncaught TypeError: Cannot set properties of null (setting ‘innerHTML’) when local development

huangapple go评论69阅读模式
英文:

Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') when local development

问题

我有以下HTML代码 -

<!DOCTYPE html>
<head>
    <title>Intro to Js</title>
    <link rel="stylesheet" type="text/css" href="css/style.css"></link>
    <script type="text/javascript" src="scripts/index.js"></script>
</head>
<body>
    
    <h1>Introduction to Javascript</h1>
    <hr/>
    <div id="ResultContainer"></div>
    
</body>
</html>

以下是javascript scripts/index.js

var resultContainer = document.getElementById("ResultContainer");
resultContainer.innerHTML = "Setting up environment";

以下是css/style.css

body {
    margin-top: 20px;
    margin-left: 20px;
}

h1 {
    font-size: 50px;
}

#ResultContainer {
    font-size: 30px;
    margin-top: 30px;
    padding: 10px;
    width: 450 px;
    height: 200px;
    border: 1px solid black;
}

当我使用start命令运行代码时,我遇到了错误

Uncaught TypeError: resultContainer is null
http://localhost:3000/scripts/index.js:6

但是在codepen中相同的代码运行正常。

有没有办法修复本地的错误?

英文:

I've below HTML code -

&lt;!DOCTYPE html&gt;
&lt;head&gt;
    &lt;title&gt;Intro to Js&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/style.css&quot;&gt; &lt;/link&gt;
    &lt;script type=&quot;text/Javascript&quot; src=&quot;scripts/index.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    
    &lt;h1&gt;Introduction to Javascript&lt;/h1&gt;
    &lt;hr/&gt;
    &lt;div id=&quot;ResultContainer&quot;&gt;&lt;/div&gt;
    
&lt;/body&gt;
&lt;/html&gt;

below javascript scripts/index.js

var resultContainer = document.getElementById(&quot;ResultContainer&quot;);
resultContainer.innerHTML = &quot;Setting up environment&quot;;

below is the css/style.css

body {
    margin-top: 20px;
    margin-left: 20px;
}

h1 {
    font-size: 50px;
}

#ResultContainer {
    font-size: 30px;
    margin-top: 30px;
    padding: 10px;
    width: 450 px;
    height: 200px;
    border: 1px solid black;
}

when I run the code using start command I get error

> Uncaught TypeError: resultContainer is null
<anonymous> http://localhost:3000/scripts/index.js:6

Uncaught TypeError: Cannot set properties of null (setting ‘innerHTML’) when local development

But the same code in codepen is running fine.

Any idea how can I fix the error in local?

答案1

得分: 2

Your code is running before the DOM is fully loaded (element does not exist).

To solve the issue you can either place the script at the bottom of the body tag or wrap your code with DOMContentLoaded which will ensure that your code executes only after the page is fully loaded:

window.addEventListener("DOMContentLoaded", (event) => {
  var resultContainer = document.getElementById("ResultContainer");
  resultContainer.innerHTML = "Setting up environment";
});
英文:

Your code is running before the DOM is fully loaded (element does not exists).

To solve the issue you can either place the script at the bottom of the body tag or wrap your code with DOMContentLoaded which will ensure that your code executes only after the page is fully loaded:

window.addEventListener(&quot;DOMContentLoaded&quot;, (event) =&gt; {
  var resultContainer = document.getElementById(&quot;ResultContainer&quot;);
  resultContainer.innerHTML = &quot;Setting up environment&quot;;
});

答案2

得分: 0




```

英文:

the script tag doesn't go in the header as it won't be able to read the boyd elements, put it in the body tag

&lt;!DOCTYPE html&gt;
&lt;head&gt;
    &lt;title&gt;Intro to Js&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/style.css&quot;&gt; &lt;/link&gt;
&lt;/head&gt;
&lt;body&gt;
    
    &lt;h1&gt;Introduction to Javascript&lt;/h1&gt;
    &lt;hr/&gt;
    &lt;div id=&quot;ResultContainer&quot;&gt;&lt;/div&gt;
    &lt;script type=&quot;text/Javascript&quot; src=&quot;scripts/index.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年7月13日 13:17:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定