日期:2014-05-17  浏览次数:20716 次

getElementById 不能获得对象。。找不出原因
本帖最后由 itakeblue 于 2013-01-10 00:29:15 编辑
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Icon</title>
</head>

<body>

<script type="text/javascript">
<!--
document.getElementById('div_test').style.display='none';
//-->
</script>

<div id="div_test">test</div>

</body>
</html>


就这样简单的代码,有点奇怪呢

浏览器提示脚本错误 '错误: 无法获取未定义或 null 引用的属性“style”'

------解决方案--------------------
顺序执行的, div还没有创建就执行完了srcipt里面的代码,所以咯。。
------解决方案--------------------
引用:
顺序执行的, div还没有创建就执行完了srcipt里面的代码,所以咯。。

------解决方案--------------------
<div id="div_test">test</div>
<script type="text/javascript">
<!--
    document.getElementById('div_test').style.display='none';
//-->
</script>