About js error Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')

Problem Description

很多人在写js代码的时候都遇到过这样的问题

insert image description here

Cannot set properties of null (setting 'innerHTML') , this error means that the attribute "innerHTML" of null cannot be read, indicating that the place where you want to insert the HTML code you want to write cannot be found.

Solution

At this time, we check the corresponding code of js according to the position of the error reminder
insert image description here. After checking, we find that we have referenced a DOM node p here. We can add a p tag to it.
insert image description hereAdd a p tag in the red box to ensure that the operation object is obtained.

summary:

  • First of all, you should make sure whether you have obtained the operation object
  • Second, consider reordering scripts or adding window.onload events

Guess you like

Origin blog.csdn.net/qq_46362763/article/details/123509540