Home  >  Article  >  Web Front-end  >  TypeError document.getElementById(...) is null error reason_javascript skills

TypeError document.getElementById(...) is null error reason_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:58:381727browse

Today I hand-wrote a simple page for testing. After writing the js part, I always reported TypeError document.getElementById(...) is null. I read the code several times and found no syntax errors. No. The solution was to search on the Internet and found that the syntax of document.getElementById for finding nodes based on ID is a bit particular. The details are as follows:

(1) If it is written in front of the node where the id is located, such as in the head block, it should be written as follows:

Copy code The code is as follows:

window.onload=function(){
......js code
}

The above code means to run js after the html element is loaded.

(2) Write it after the node where the id is located, otherwise it will report TypeError: document.getElementById(...) is null, which cannot find the node.

If you write too much jquery code, it is easy to overlook some js details, so record them for future reference.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn