Home > Web Front-end > JS Tutorial > Solution to document.getElementById getting the control object empty_Basic knowledge

Solution to document.getElementById getting the control object empty_Basic knowledge

WBOY
Release: 2016-05-16 17:13:45
Original
1849 people have browsed it

1. The following is a simple example that displays a piece of information when the page loads

Copy code The code is as follows:

<%@ page language="java" import= "java.util.*" pageEncoding="UTF-8"%>







Before the page is loaded, some information will be output



< ;/html>

After execution, it is indeed "information is output before the page is loaded."
2. In the following example, document.getElementById will be empty.
My plan is: when the page loads, the data processed by the background is displayed in the text box of , such as the string "hello, my friend!" here. But what is read here through document.getElementById is that the object is empty.
Because the onLoad method is executed before the page is loaded, the text box corresponding to the text box with id="mes" has not yet been loaded.

Copy code The code is as follows:

<%@ page language="java" import= "java.util.*" pageEncoding="UTF-8"%>


🎜> 



  the message is:





3. Solution
When loading an Html web page, all data in will be loaded. Load first, then .
So we can just write javascript after and before . The program is executed sequentially and can be executed until the corresponding javascript call is executed. Not using the onLoad method. The code is as follows:




Copy code

The code is as follows:<%@ page language="java" import= "java.util.*" pageEncoding="UTF-8"%>

the message is:



                                                                                                                                                                                                                                                           t. value="hello, my friend!"
}
showMessage(); //Call the method and update the text box

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template