为什么document.GetElementById返回null [英] Why is document.GetElementById returning null

查看:153
本文介绍了为什么document.GetElementById返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在成功使用document.GetElementById但是从一段时间以来我无法再次使用它。
我使用它的旧页面仍然可以工作,但事情很简单:

I've been using document.GetElementById succesfully but from some time on I can't make it work again. Old pages in which I used it still work but things as simple as this:

<html>
<head>
 <title>no title</title> 
 <script type="text/javascript">
 document.getElementById("ThisWillBeNull").innerHTML = "Why is this null?";
 </script>
</head>
<body>
 <div id="ThisWillBeNull"></div>
</body>
</html>

现在一直给我document.getElementById(parsedOutput)为null。
如果我使用Firefox或Chrome或我启用了哪些扩展程序或者我用于html的标题,它无关紧要,它始终为空,我找不到可能出错的地方。

Are giving me "document.getElementById("parsedOutput") is null" all the time now. It doesnt matter if I use Firefox or Chrome or which extensions i have enabled or what headers I use for the html, it's always null and I can't find what could be wrong.

感谢您的输入=)

推荐答案

页面从上到下呈现。您的代码在解析后立即执行。在执行时,div尚不存在。你需要将它包装在window.onload函数中。

The page is rendered top to bottom. You code executes immediately after it's parsed. At the time of execution, the div does not exist yet. You need to wrap it in an window.onload function.

这篇关于为什么document.GetElementById返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆