本文介绍了即使元素存在 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
发布评论