即使已定义,“ appendChild”也为null

问题描述

我正在尝试创建新元素以显示Im不和谐机器人列表的信息

  var btn = document.createElement("BUTTON");
  btn.innerHTML = "Try It"
  document.body.appendChild(btn);
<html>
  <head>
    <title>Squox Bot List</title>
        <script src="script.js"></script>
    <link href="style.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Vast+Shadow&display=swap" rel="stylesheet">
  </head>
  <body>
        <div class="line">
            <a target="_blank" href="https://discord.gg/TUD9WPE"><img src="images/discord.png" width="50" height="auto"></a>
            <h1>Squox Bots</h1><br>
      </div>
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
  </body>
</html>

我一直在获取无法读取null的属性'appendChild'
当我从Stackoverflow查看旧问题时,会遇到相同的错误

所以有帮助的人谢谢你

解决方法

我通过使JS函数在身体加载时运行来解决了我的错误