即使元素存在 getElementById() 也返回 null [英] getElementById() returns null even though the element exists
本文介绍了即使元素存在 getElementById() 也返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 getElementById() 获取元素,但即使该元素存在,它也会返回 null.我做错了什么?
<头><title>等等</title><script type="text/javascript">警报(document.getElementById(abc"));头部><身体><div id="abc">
解决方案
你必须把它放在一个 document
load
事件中.到脚本执行时,DOM 还没有到达 abc
.
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>
解决方案
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屋!
查看全文