(Original) JS click event - Uncaught TypeError: Cannot set property 'onclick' of null

html part of the code:

 

JS part of the code:

The effect that needs to be achieved: Click on the picture to switch back and forth.

The wrong way I started: The code looks like the picture above (logically it looks like there is no error)

Trying process: It is no problem to put the JS code before the closing tag of </body>, write it in the JS code file separately, import the JS text from the head tag, and report the error Uncaught TypeError: Cannot set property 'onclick' of null

Final solution: Put the JS code into window.load = function(){.........}, and introduce the JS text into the head tag, so it can run correctly. O(∩_∩)O

The second method: As mentioned in the above attempt, write the JS code before the </body> tag

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325272122&siteId=291194637