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
Document. all is a proprietary attribute of IE 4.0 and later versions. It is a group of all objects in the current document, including not only object objects visible on the page, but also invisible objects, for example, html annotations. In the
View code
1 $. Ajax ({2 type: "Post", // Request Method3 URL: "text.txt", // address, which is the Action Request Path4 cache: false,5 Data: "text", // data type text xml json script jsonp6 success: function (MSG) {// The returned parameter is
However, if you need to find a specific element in the document, the most effective method is getelementbyid ().
However, when using getelementbyid, The results may be different for different browsers.
CopyCode The Code is as follows: text1:
Text2
Use your brain as a browser to execute the following code twice: IE6 and IE9:
Copy codeThe Code is as follows: function testFunc (){
Alert ('test ')
}
$ (Function (){
Var g = document. getElementById,
W = window. testFunc;
// G
Alert (typeof (g
1. 'null' is null or not an object: Add the defer = true attribute to it,That is: 2. document. getElementById is null Cause: var titab = document. getElementById ("titletab"); no object retrievedSolution:1. Add alert (titab) under the error
However, if you need to find a specific element in the document, the most effective method is getElementById ().However, when using getElementById, The results may be different for different browsers.Copy codeThe Code is as follows:Text1:Text2:I
Today, I wrote a webpage. When I want to dynamically display data from other programs in the background when loading onLoad on the page, it is always null to use document. getElementById to get the control object. But check that this id does exist.
// Code snippet
VaR d = Document. getelementbyid ("billno ");
Document is a built-in object that can be directly used on the page without the need to declare and define it.
Getelementbyid:
Purpose: Generally, the ID on the page is unique and used
Difference between document. forms [0] And getElementByName in JavaScript, getelementbyid
First, let's look at an example:
Copy codeThe Code is as follows: testtest testtest testtest
When document. forms [0] has one or more form forms in the HTML
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.