简体   繁体   English

如何解决未捕获的类型错误:尝试使用 JS 和 html 显示当前时间时,无法在打印时间设置 null 的属性(设置“innerHTML”)

[英]How to solve Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') at printTime when trying to show current time with JS and html

error错误

function printTime() {
  let clock = document.getElementById("clock"); // use dom to choose a place for the clock
  let curr = new Date();
  let currtime =
    curr.getFullYear() + "/" + (curr.getMonth() + 1) + "/" + curr.getDate();
  clock.innerHTML = currtime;
  setTimeout("printTime()", 1000);
}

window.onload = function () {
  printTime();
};

It seems like the time displays correctly but I am curious why am I getting this error: lab6.js:6 Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') at printTime似乎时间显示正确,但我很好奇为什么会出现此错误:lab6.js:6 Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') at printTime

when I also added <div id="clock"></div> in the HTML code?当我还在 HTML 代码中添加<div id="clock"></div>时?

I think your code is working fine here我认为你的代码在这里工作正常

 function printTime() { let clock = document.getElementById("clock"); // use dom to choose a place for the clock let curr = new Date(); let currtime = curr.getFullYear() + "/" + (curr.getMonth() + 1) + "/" + curr.getDate(); clock.innerHTML = currtime; setTimeout("printTime()", 1000); } window.onload = function () { printTime(); };
 <div id="clock"></div>

In my opinion you get the error because the element is not yet rendered on the window.onload .在我看来,您收到错误是因为该元素尚未在window.onload上呈现。 Instead you should be looking for body.onload , or in other words, if you would set the call to printTime directly in the body tag, it would work fine.相反,您应该寻找body.onload ,或者换句话说,如果您直接在 body 标签中设置对printTime的调用,它会正常工作。

<body onload="printTime()">

In any case, wrapping the printTime() inside an arrow function also works without the error :在任何情况下,将printTime()包装在箭头函数中也不会出现错误:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
    <script>
      function printTime() {
        let clock = document.getElementById("clock"); // use dom to choose a place for the clock
        let curr = new Date();
        let currtime = curr.getFullYear() + "/" + (curr.getMonth() + 1) + "/" + curr.getDate();
        clock.innerHTML = currtime;
        setTimeout("printTime()", 1000);
      }

      window.onload = () => {
        printTime();
      };
    </script>
  </head>
  <body>
    <div id="clock"></div>
  </body>
</html>

暂无
暂无

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

相关问题 JS 中的错误“Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') at call.html:10:58” - Error in JS "Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') at call.html:10:58" 错误:未捕获类型错误:无法设置 null 的属性(设置“innerHTML”) - Error: Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') 未捕获(承诺中)类型错误:无法设置 null 的属性(设置“innerHTML”) - Uncaught (in promise) TypeError: Cannot set properties of null (setting 'innerHTML') Javascript 未捕获类型错误:无法设置 null 的属性(设置“innerHTML”) - Javascript Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') 语法中的innerHTML 错误不起作用; 未捕获的类型错误:无法设置 null 的属性(设置“innerHTML”) - innerHTML error in syntax not working; Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') HTML,JS,接收未捕获的类型错误:当变量具有值时,无法将属性“innerHTML”设置为 null - HTML, JS, receiving Uncaught TypeError: Cannot set property 'innerHTML' of null when variable has value script.js:3 Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') 为什么我一次又一次地得到这个 - script.js:3 Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') why am i getting this again and again 未被捕获的TypeError:将innerhtml设置为数组中的字符串时,无法设置null的属性“ innerHTML” - Uncaught TypeError: Cannot set property 'innerHTML' of null- when setting the innerhtml to a string from an array 未捕获的类型错误:为隐藏字段设置 innerhtml 时,无法将属性“innerHTML”设置为 null - Uncaught TypeError: Cannot set property 'innerHTML' of null when setting innerhtml for hiddenfield document.getElementById 未捕获的类型错误:无法设置 null 的属性(设置“innerHTML”) - document.getElementById Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM