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
Table of contents Supports multiple types of filt...
1. Introduction The telnet command is used to log...
Introduction to MySQL logical architecture Overvi...
The full name of Blog should be Web log, which me...
<br />Navigation does not just refer to the ...
When there are tens of thousands of records in th...
<style type="text/css"> Copy code ...
Create a simple Spring boot web project Use the i...
Div solution when relative width and absolute wid...
background When performing a SQL query, I tried t...
background We can use react-color to implement th...
1. Refer to the official website to install docke...
Without further ado, I will post the code for you...
Batch replace part of the data of a field in MYSQ...
This article describes how to install mysql5.6 us...