欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Cannot set property 'innerHTML' of null

 

Why do I get an error or Uncaught TypeError: Cannot set property 'innerHTML' of null? I thought I understood innerHTML and had it working before.

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>

<script type ="text/javascript">
    what();
    function what(){
        document.getElementById('hello').innerHTML = 'hi';
    };
</script>

</head>

<body>
<div id="hello"></div>
</body>
</html>
正确答案 
即必须在 id="hello" 后才能执行这个 js 否则 
document.getElementById('hello') 找不到
You have to place the hello div before the script, so that it exists when the script is loaded.

js编程浏览器总是报错 Uncaught TypeError: Cannot set property 'innerHTMl' of null 5

我的源码是:
<body >
<div id="restt"></div> 
<script type="text/javascript">
window.document.getElementById("restt").innerHTMl="abc";
Uncaught TypeError: Cannot set property 'innerHTMl' of null
</script>
</body >
136****3932 | 浏览 29869 次
发布于2013-08-22 12:05最佳答案
 
<div id="restt"></div>  里面没有元素; 你要放显示abc的标签进去吧;如label
 
追问
能不能代码贴出来,看一看啊,大神
 
追答
<body >
    <div id="restt"></div> 
 </body >
 <script type="text/javascript">
window.document.getElementById("restt").innerHTMl="abc";
</script>
把js放在最body后面,让body元素都加载,在执行你的js
 
追问
我试过了,网上都这么说,但是好像木有多大效果,不管怎样吧,还是感谢各位大神!
来自  https://zhidao.baidu.com/question/583825697.html
普通分类: