欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

JS代码在ie8里报错document.getElementById(...)为空或不是对象的解决方法

程序员文章站 2022-05-16 16:08:16
...
ie8中对于声明文档类型更加严格如下面的代码。
<input type='text' name='os' value='Windows平台' style='width:100px' /> 
<script type="text/javascript"> 
                function osdo(str){ 
                document.getElementById("os").value=str; 
                } 
                </script> 
<input name="ostmp" type="radio" value="" onclick="osdo('')" />清空 
<input name="ostmp" type="radio" value="Windows平台" onclick="osdo('Windows平台')" /> 
Windows平台 <input name="ostmp" type="radio" value="" onclick="osdo('S60v2')" />S60v2 
<input name="ostmp" type="radio" value="" onclick="osdo('S60v3')" />S60v3 
<input name="ostmp" type="radio" value="" onclick="osdo('CHM')" />CHM 
<input name="ostmp" type="radio" value="" onclick="osdo('PDF')" />PDF 
<input name="ostmp" type="radio" value="" onclick="osdo('PPT')" />PPT 
<input name="ostmp" type="radio" value="" onclick="osdo('DOC')" />WORD 
<input name="ostmp" type="radio" value="" onclick="osdo('EXCEL')" />EXCEL
 
<input name="ostmp" type="radio" value="" onclick="osdo('asp')" />asp 
<input name="ostmp" type="radio" value="" onclick="osdo('asp+access')" />asp+access 
<input name="ostmp" type="radio" value="" onclick="osdo('asp+sqlserver')" />asp+sqlserver 
<input name="ostmp" type="radio" value="" onclick="osdo('asp+sql/access')" /> 
asp+sql/access <strong>php源码</strong> 
<input name="ostmp" type="radio" value="" onclick="osdo('php')" />php 
<input name="ostmp" type="radio" value="" onclick="osdo('php+mysql')" /> 
php+mysql<input name="ostmp" type="radio" value="" onclick="osdo('jsp')" />jsp <strong>asp.net源码</strong> 
<input name="ostmp" type="radio" value="" onclick="osdo('asp.net')" />asp.net 
<input name="ostmp" type="radio" value="" onclick="osdo('asp.net+access')" />asp.net+access 
<input name="ostmp" type="radio" value="" onclick="osdo('asp.net+sql')" />asp.net+sql 
<input name="ostmp" type="radio" value="" onclick="osdo('asp.net+sql/access')" />asp.net+sql/access

可以看到这段代码在ie7与ie8中都是可以运行的,下面的代码我们加入声明文档

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

加了上面的文档类型声明。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
运行环境: 
<input type='text' name='os' value='Windows平台' style='width:100px' /> 

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

发现 提示document.getElementById(...) 为空或不是对象的解决方法
如果页面中既有文档声明,因为用div+css布局的时候,声不声明文档,页面的一些地方都不一样。
可以在head中添加下面的代码

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

完整的代码

<!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" /> 
运行环境: 
<input type='text' name='os' value='Windows平台' style='width:100px' /> 

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