即使元素存在,getElementById()也会返回null [英] getElementById() returns null even though the element exists
本文介绍了即使元素存在,getElementById()也会返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用getElementById()获取元素,但即使元素存在,它也会返回null。我做错了什么?
I'm trying to get the element with getElementById(), but it returns null even though the element exists. What am I doing wrong?
<html>
<head>
<title>blah</title>
<script type="text/javascript">
alert(document.getElementById("abc"));
</script>
</head>
<body>
<div id="abc">
</div>
</body>
推荐答案
你必须把它放在文档
load
事件中。在执行脚本时,DOM没有得到 abc
。
You have to put this in a document
load
event. The DOM hasn't gotten to abc
by the time the script is executed.
这篇关于即使元素存在,getElementById()也会返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文