无法设置null的属性'innerHTML' [英] Cannot set property 'innerHTML' of null

查看:140
本文介绍了无法设置null的属性'innerHTML'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我会收到错误或未捕获的TypeError:无法设置null的属性'innerHTML'?
我以为我理解innerHTML并让它在以前工作。

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>


推荐答案

你必须在脚本之前放置hello div,以便在加载脚本时存在。

You have to place the hello div before the script, so that it exists when the script is loaded.

这篇关于无法设置null的属性'innerHTML'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆