TypeError document. getElementById (...) is null error cause, typeerrornull
Today, I wrote a simple page for testing. After writing JavaScript, I always reported TypeError document. getElementById (...) is null error. The Code has been read several times and no syntax error is found. No way, I searched the internet to find the document. the syntax of getElementById for finding nodes by id is still a bit exquisite, as follows:
(1) If it is written in front of the node where the id is located, such as the head block, it should be written as follows:
Copy codeThe Code is as follows:
Window. onload = function (){
...... Js Code
}
The above code means to run js after html elements are loaded.
(2) Write it behind the node where the id is located. Otherwise, the error "TypeError: document. getElementById (...) is null" cannot be found.
When jquery code is too much written, it is easy to ignore some js details and record a record for later forgetting.