Cannot read property *0* of null

前端 未结 4 1592
长发绾君心
长发绾君心 2021-01-13 08:22

I get this error \"Cannot read property \'0\' of null.\"

I have a table with

somename.html

4条回答
  •  一向
    一向 (楼主)
    2021-01-13 08:48

    When you call setInterval, you must supply a function. You're calling the function immediately, not passing the function to setInterval. It should be:

    setInterval(test, 1000);
    

    Since you're calling it immediately, it's running before the DOM has loaded fully, and the td1 element isn't found. So getElementById returns null instead of the element.

    In addition, as the other answers pointed out, you should not use [0] to access the element returned by getElementById. It returns a single element, not a NodeList.

提交回复
热议问题