在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
分享到:
相关推荐
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错误原因,这是很容易犯的一个低级错误,需要的朋友可以参考下
document.getElementById()对象和使用方法
document.getElementById为null 原因: var titab=document.getElementById(“titletab”);没有取到对象 解决办法: 1.在错误语句的下面加上alert(titab);调试语句,看看是否有消息框弹出 2.如果没有弹出,检查...
document.getElementById(vImgs).innerHTML="<span style=color:red;font-size:12px>"+16+"</span>"; } else{ document.getElementById(vImgs).innerHTML="<span style=color:red;font-size:12px>"+(16...
23.弹出输入提示框:window.prompt(); 24.指定当前显示链接的位置:window.location.href= "URL " 25.取出窗体中的所有表单的...29.得到元素的方法:document.getElementById() 30.设置表单中所有文本型的成员的值为空:
5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID号").name(或value) 6.一个小写转大写的JS: document.getElementById("output").value = document.getElementById("input").value.toUpperCase...
2.JS中的注释为// 3.传统的HTML文档顺序是: document->html->(head,body) 4.一个浏览器窗口中的DOM顺序是: window->(navigator,screen,history,location,document) 5.得到表单中元素的名称和值: document....
5.得到表单中元素的名称和值:document.getElementById( "表单中元素的ID號 ").name(或value) 6.一个小写转大写的JS: document.getElementById( "output ").value = document.getElementById( "input ").value....
主要是对各浏览器对document.getElementById等方法的实现差异进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助
5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID號").name(或value) 6.一个小写转大写的JS: document.getElementById("output").value = document.getElementById("input").value.toUpperCase...
5.得到表单中元素的名称和值:document.getElementById( "表单中元素的ID號 ").name(或value) 6.一个小写转大写的JS: document.getElementById( "output ").value = document.getElementById( "input ").value....
<script type=text/javascript> function setTab03Syn ( i ) { selectTab03Syn(i); } function selectTab03Syn ( i ) { switch(i){ case 1: document.getElementById("TabTab03Con1").style....
5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID号").name(或&#118alue) 6.一个小写转大写的&#106s: document.getElementById("output").&#118alue = document.getElementById("input").&...
document.getElementById("EleId").style.visibility="hidden"; document.getElementById("EleId").style.visibility="visible"; 利用上述方法实现隐藏后,页面的位置还被控件占用,显示空白。 方法二: ...
IE方法:document.frames[‘myFrame’].document.getElementById(‘test’).value; 火狐方法:document.getElementById(‘myFrame’).contentWindow.document.getElementById(‘test’).value; IE、火狐方法: 代码...
5.得到表单中元素的名称和值:document.getElementById("表单中元素的ID號").name(或value) 6.一个小写转大写的JS: document.getElementById("output").value = document.getElementById("input").value.toUpperCase...
今天手写了一个简单的页面做测试用,写完js部分总是报 TypeError document.getElementById(…) is null 这个错误,代码看了几遍,语法上没找着什么错误,没办法,就在互联网上搜索了一下,才发现 document....
5.得到表单中元素的名称和值:document.getElementById( "表单中元素的ID號 ").name(或value) 6.一个小写转大写的JS: document.getElementById( "output ").value = document.getElementById( "input ").value....