getElementById cannot get the object There is a sequence when the browser parses the document. Before the page is loaded, or before the corresponding DOM object is loaded, the corresponding object cannot be obtained. Look at the following code: <script> var temp = document.getElementById("div"); alert(temp); </script> <body> <div id="div"> <input name="username" id="username" type="text"> <button id="btn">Button</button> </div> </body> In this code, This is because when the browser parses the code in the script tag, the DOM elements in the body have not yet been loaded, so naturally nothing can be retrieved. Solution: Move the code in the script to after the body element. <body> <div id="div"> <input name="username" id="username" type="text"> <button id="btn">Button</button> </div> <script> var temp = document.getElementById("div"); alert(temp); </script> </body> Or add window.onload <script> window.onload = function(){ var temp = document.getElementById("div"); alert(temp); } </script> Summarize The above is the editor's introduction to solving the problem that the document.getElementBy series methods cannot obtain objects. I hope it will be helpful to everyone. Thank you very much for your support of the 123WORDPRESS.COM website! |
>>: HTML solves the problem of invalid table width setting
Often when we open foreign websites, garbled char...
Preface With the development of big front-end, UI...
Table of contents 1. Download JDK (take jdk1.8.0 ...
Let's take a look at the problem of VScode re...
Table of contents 1. async 2. await: 3. Comprehen...
It is mainly a CSS style control and a META tag; C...
I would like to share the installation and config...
1. Download the zip archive version from the offi...
1. Install Apache $ sudo apt update && su...
Because of network isolation, MySQL cannot be ins...
When the server needs to be started during develo...
The vue project implements an upgraded version of...
As the number of visits to the company's webs...
<br />For some time, I found that many peopl...
Copy code The code is as follows: <object id=&...