Home >Web Front-end >JS Tutorial >JS code reports an error in ie8 that document.getElementById(...) is empty or not an object. Solution

JS code reports an error in ie8 that document.getElementById(...) is empty or not an object. Solution

PHP中文网
PHP中文网Original
2016-05-16 18:40:581764browse

IE8 is more strict about declaring document types, as shown in the following code.

<input type=&#39;text&#39; name=&#39;os&#39; value=&#39;Windows平台&#39; style=&#39;width:100px&#39; /> 
<script type="text/javascript"> 
                function osdo(str){ 
                document.getElementById("os").value=str; 
                } 
                </script> 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;&#39;)" />清空 
<input name="ostmp" type="radio" value="Windows平台" onclick="osdo(&#39;Windows平台&#39;)" /> 
Windows平台 <input name="ostmp" type="radio" value="" onclick="osdo(&#39;S60v2&#39;)" />S60v2 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;S60v3&#39;)" />S60v3 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;CHM&#39;)" />CHM 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;PDF&#39;)" />PDF 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;PPT&#39;)" />PPT 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;DOC&#39;)" />WORD 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;EXCEL&#39;)" />EXCEL
 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;asp&#39;)" />asp 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;asp+access&#39;)" />asp+access 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;asp+sqlserver&#39;)" />asp+sqlserver 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;asp+sql/access&#39;)" /> 
asp+sql/access <strong>php源码</strong> 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;php&#39;)" />php 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;php+mysql&#39;)" /> 
php+mysql<input name="ostmp" type="radio" value="" onclick="osdo(&#39;jsp&#39;)" />jsp <strong>asp.net源码</strong> 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;asp.net&#39;)" />asp.net 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;asp.net+access&#39;)" />asp.net+access 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;asp.net+sql&#39;)" />asp.net+sql 
<input name="ostmp" type="radio" value="" onclick="osdo(&#39;asp.net+sql/access&#39;)" />asp.net+sql/access

You can see that this code can run in both ie7 and ie8. We add the declaration document to the following code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

and add the above document type declaration.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
运行环境: 
 

 
清空 
 
Windows平台 S60v2 
S60v3 
CHM 
PDF 
PPT 
WORD 
EXCEL
 
asp 
asp+access 
asp+sqlserver 
 
asp+sql/access php源码 
php 
 
php+mysqljsp asp.net源码 
asp.net 
asp.net+access 
asp.net+sql 
asp.net+sql/access

The solution is found when the prompt document.getElementById(...) is empty or not an object
If there is a document declaration in the page, because when using div css layout, the document is not declared. , some parts of the page are different.
You can add the following code in the head

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

Complete code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> 
运行环境: 
 

 
清空 
 
Windows平台 S60v2 
S60v3 
CHM 
PDF 
PPT 
WORD 
EXCEL
 
asp 
asp+access 
asp+sqlserver 
 
asp+sql/access php源码 
php 
 
php+mysqljsp asp.net源码 
asp.net 
asp.net+access 
asp.net+sql 
asp.net+sql/access


Statement:
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