`

js问题:document.getElementById 为null的解决方案

阅读更多

在IE8下运行如下代码,出现document.getElementById缺少对象的问题。

<html>
 <head>
  <title> Javascript问题测试</title>  
  <script language="javascript">  		
		function test(){
			alert(document.getElementById("b"));
		}
		test();
  </script>
 </head>
 <body>
	<div id="a">
		<span id="b">测试</span>
	</div>	
 </body>
</html>

 

运行结果如图: 

 

解决方案:把JS代码放在要获取的元素的后面

<html>
 <head>
  <title> Javascript问题测试</title>  
  
 </head>
 <body>
	<div id="a">
		<span id="b">测试</span>
	</div>
	<script language="javascript">  		
		function test(){
			alert(document.getElementById("b"));
		}
		test();
	</script>
 </body>
</html>

 

总结:出现问题的原因是 HTML 从上至下 解析 , 应该先 有"对象" ,再去"引用".

 
  • 大小: 4.1 KB
1
1
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    repostorie.jar

    javascript:document.getElementById('downLoad').action='/download.php?fileid=15775884';downishare('0');javascript:document.getElementById('downLoad').action='/download.php?fileid=15775884';downishare('...

    TypeError document.getElementById(...) is null错误原因

    主要介绍了TypeError document.getElementById(...) is null错误原因,这是很容易犯的一个低级错误,需要的朋友可以参考下

    document.getElementById()

    document.getElementById()对象和使用方法

    document.getElementById为空或不是对象的解决方法

    document.getElementById为null 原因: var titab=document.getElementById(“titletab”);没有取到对象 解决办法: 1.在错误语句的下面加上alert&#40;titab&#41;;调试语句,看看是否有消息框弹出 2.如果没有弹出,检查...

    js 密碼 強弱 的三種狀態檢測

    document.getElementById(vImgs).innerHTML="&lt;span style=color:red;font-size:12px&gt;"+16+"&lt;/span&gt;"; } else{ document.getElementById(vImgs).innerHTML="&lt;span style=color:red;font-size:12px&gt;"+(16...

    javascript常用100语句

    23.弹出输入提示框:window.prompt(); 24.指定当前显示链接的位置:window.location.href= "URL " 25.取出窗体中的所有表单的...29.得到元素的方法:document.getElementById() 30.设置表单中所有文本型的成员的值为空:

    总结常用JavaScript语法107条

    5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID号").name(或value) 6.一个小写转大写的JS: document.getElementById("output").value = document.getElementById("input").value.toUpperCase...

    JavaScript

    2.JS中的注释为// 3.传统的HTML文档顺序是: document-&gt;html-&gt;(head,body) 4.一个浏览器窗口中的DOM顺序是: window-&gt;(navigator,screen,history,location,document) 5.得到表单中元素的名称和值: document....

    Javascript的常用语句

    5.得到表单中元素的名称和值:document.getElementById( "表单中元素的ID號 ").name(或value) 6.一个小写转大写的JS: document.getElementById( "output ").value = document.getElementById( "input ").value....

    各浏览器对document.getElementById等方法的实现差异解析

    主要是对各浏览器对document.getElementById等方法的实现差异进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助

    107个javascript语句

    5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID號").name(或value) 6.一个小写转大写的JS: document.getElementById("output").value = document.getElementById("input").value.toUpperCase...

    107个常用Javascript语句

    5.得到表单中元素的名称和值:document.getElementById( "表单中元素的ID號 ").name(或value) 6.一个小写转大写的JS: document.getElementById( "output ").value = document.getElementById( "input ").value....

    不规则TAB选项卡效果

    &lt;script type=text/javascript&gt; function setTab03Syn ( i ) { selectTab03Syn(i); } function selectTab03Syn ( i ) { switch(i){ case 1: document.getElementById("TabTab03Con1").style....

    常用javascript语句大全

    5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID号").name(或&#118alue) 6.一个小写转大写的&#106s: document.getElementById("output").&#118alue = document.getElementById("input").&...

    JS控制HTML元素的显示和隐藏的两种方法

    document.getElementById("EleId").style.visibility="hidden"; document.getElementById("EleId").style.visibility="visible";  利用上述方法实现隐藏后,页面的位置还被控件占用,显示空白。  方法二: ...

    javascript 获取iframe里页面中元素值的方法

    IE方法:document.frames[‘myFrame’].document.getElementById(‘test’).value; 火狐方法:document.getElementById(‘myFrame’).contentWindow.document.getElementById(‘test’).value; IE、火狐方法: 代码...

    常用Javascript语句

    5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID號").name(或value) 6.一个小写转大写的JS: document.getElementById("output").value = document.getElementById("input").value.toUpperCase...

    TypeError document.getElementById(…) is null错误原因

    今天手写了一个简单的页面做测试用,写完js部分总是报 TypeError document.getElementById(…) is null 这个错误,代码看了几遍,语法上没找着什么错误,没办法,就在互联网上搜索了一下,才发现 document....

    107个常用Javascript语句.doc

    5.得到表单中元素的名称和值:document.getElementById( "表单中元素的ID號 ").name(或value) 6.一个小写转大写的JS: document.getElementById( "output ").value = document.getElementById( "input ").value....

Global site tag (gtag.js) - Google Analytics