未捕获的类型错误:无法读取属性“innerHTML”为空
谁能解释一下这个错误是什么?
未捕获类型错误:无法读取 null 的属性“innerHTML”
这是导致问题的行:
var idPost=document.getElementById("status").innerHTML;
Can anyone explain what is this error?
Uncaught TypeError: cannot read property 'innerHTML' of null
This is the line which is causing the issue:
var idPost=document.getElementById("status").innerHTML;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您的网页中不存在“状态”元素。
所以 document.getElementById("status") 返回 null。虽然不能使用NULL的innerHTML属性。
您应该添加这样的条件:
The 'status' element does not exist in your webpage.
So document.getElementById("status") return null. While you can not use innerHTML property of NULL.
You should add a condition like this:
更新:
这个问题不要求jquery。因此,让我们在不使用 jquery 的情况下完成此操作:
请注意,此方法不适用于 IE8。
旧答案:
您在 DOM 准备好之前调用此脚本。如果你将此代码写入 jquery 的 $(function() 方法中,它将起作用。
Update:
The question doesn't ask for jquery. So lets do it without jquery:
Note this method will not work on IE8.
Old Answer:
You are calling this script before DOM is ready. If you write this code into jquery's $(function() method it will work.
如果脚本位于 HTML 文档的头部,则浏览器尚未创建 HTML 文档的正文,无论最终会出现什么(如果您的脚本位于 HTML 文件中但位于 HTML 文档的上方,则会出现相同的结果)元素)。当您的变量尝试查找 document.getElementById("status") 时,它尚不存在,因此它返回 null 值。当您稍后在代码中使用该变量时,将使用初始值 (null),而不是当前值,因为没有任何内容更新该变量。
我不想将我的脚本链接移出 HTML 头,所以我在 JS 文件中执行了此操作:
在 HTML 文件中执行此操作:
如果您已经有 onload 函数,则只需添加附加行到它或调用该函数。
如果您不希望该变量是全局变量,请在您尝试运行的函数中本地定义它,并确保在页面完全加载之前不会调用该函数。
If the script is in the head of your HTML document, the body of your HTML document has not yet been created by the browser, regardless of what will eventually be there (the same result occurs if your script is in the HTML file but above the element). When your variable tries to find document.getElementById("status") it does not yet exist, and so it returns a value of null. When you then use the variable later in your code, the initial value (null) is used and not the current one, because nothing has updated the variable.
I didn't want to move my script link out of the HTML head, so instead I did this in my JS file:
And this in the HTML file:
If you already have an onload function in place, you can just add the additional line to it or call the function.
If you don't want the variable to be global, define it locally in the function that you are trying to run and make sure the function is not called before the page has fully loaded.
我有一个类似的问题,但我有现有的 id,正如 egiray 所说,我在加载 DOM 之前调用它,Javascript 控制台显示相同的错误,所以我尝试了:
它开始工作。
I had a similar problem, but I had the existing id, and as egiray said, I was calling DOM before it loaded and Javascript console was showing the same error, so I tried:
and it starts working.
在 body 语句中的 html 结构之后运行代码
run the code after your html structure in the body statement
虽然理想情况下您应该突出显示导致错误的代码并将其发布在您的问题中,但该错误是因为您试图获取“status”元素的内部 HTML:
但是“status”元素不存在 在 HTML 中 - 添加必要的元素或更改您尝试查找的 ID 以指向有效元素。
While you should ideally highlight the code which is causing an error and post that within your question, the error is because you are trying to get the inner HTML of the 'status' element:
However the 'status' element does not exist within your HTML - either add the necessary element or change the ID you are trying to locate to point to a valid element.
您是否在
#status
元素之前声明标记?
如果这样做,将首先执行该标记中的语句,并且现在不存在 id 为
status
的元素。最好的方法是将此脚本标记放在
标记之前。
Are you declaring the
<script>
tag before the#status
element?If you do, the statement in that tag will be executed first, and now there is no element with the id
status
.The best way is to put this script tag right before the
</body>
tag.看起来脚本在 DOM 加载之前执行。
尝试异步加载脚本。
Looks like the script executes before the DOM loads.
Try loading the script asynchronously.
我在 GitHub 上进行案例研究时使用的文件也遇到了同样的错误。我注意到发生此错误是因为外部 JavaScript 文件声明是在 HTML 文件的
字段中定义的。在 HTML 文件中的
字段末尾声明外部 JavaScript 文件时,可能导致此问题的情况得到解决。
I got the same error on a file I used while doing a case study on GitHub. I noticed that this error occurs because external JavaScript file declarations are defined in the
<head></head>
field in the HTML file. A situation that could cause this issue is resolved when external JavaScript files are declared at the end of the<body></body>
field in the HTML file.如果在 html 文件中找不到 id
"status"
,则会显示错误。在 html 文件中添加
"status"
id。例如:If the id
"status"
is not found in the html file it shows the error.Add the
"status"
id in your html file. For example:我犯了一个错误,我希望其他这样做的人能向我学习。我遇到了同样的错误:
我犯的错误是将一个 Javascript 文件用于两个 HTML 文件。我为每个 HTML 创建了单独的文件,效果非常好。
There is a mistake I've done and I hope anyone else doing this will learn from me. I got the same error :
The mistake I did was to use one Javascript file for two HTML files. I created individual files for each of my HTML and it worked awesomely.