简体   繁体   English

即使在我的正文标记中的所有元素之后使用脚本标记,也无法读取 null 的属性(读取“innerHTML”)

[英]Cannot read properties of null (reading 'innerHTML') Even With Script Tag After All Elements In My Body Tag

I keep getting the Uncaught TypeError: Cannot read properties of null (reading 'innerHTML') error in my console with the script tag at the end of my body tag beneath all divs and other elements.我在控制台中不断收到Uncaught TypeError: Cannot read properties of null (reading 'innerHTML')错误,脚本标签位于所有div和其他元素下方的body 标签末尾。 The error is only on user & opponent divs in the HTML and with the userPic.innerHTML and oppPic.innerHTML该错误仅出现在 HTML 以及userPic.innerHTMLoppPic.innerHTML中的用户和对手 div 上

  <body>

        <div class="container">

            <div class="header">
                <h1 class="title">Codys Pokemon Game</h1>
            </div>

            <div id="game" class="game">
                <input class="gameBtn" type="button" onclick="choose1()" value="CLICK TO START">
                <input class="infoBtn" type="button" onclick="info()" value="CLICK FOR GAME INFO">
            </div>

            <div class="user"></div>
            <div class="opponent"></div>

        </div>
    <script src="script.js"></script>
  </body>

My script.js file shouldn't have any errors but if it does, this is the part where I add a bunch of stuff to my HTML:我的script.js文件不应该有任何错误,但如果有,这是我向 HTML 添加一堆东西的部分:

    gameHTML.innerHTML = `<h2 class="gameInfo">Choose Your Attack</h2>`
    gameHTML.innerHTML += `<div class="userHp"><br>Your Health: ${pokemon1[1]}</div>`
    gameHTML.innerHTML += `<input class="att1" type="button" onclick="attack1()" value="Use: Attack: ${curPmon[4]}">`
    gameHTML.innerHTML += `<input class="att2" type="button" onclick="attack2()" value="Use: Attack: ${curPmon[5]}">`
    gameHTML.innerHTML += `<input class="att3" type="button" onclick="attack3()" value="Use: Attack: ${curPmon[6]}">`
    gameHTML.innerHTML += `<div class="oppHp"><br>Opponent Health: ${curOpp[1]}</div>`
    userPic.innerHTML += curPmon[7]
    oppPic.innerHTML += `<img src="${curOpp[7]}" alt="${curOpp[0]}">`

These are the variables I have for the IDs:这些是我的 ID 变量:


var gameHTML = document.getElementById('game')
var userPic = document.getElementById('user')
var oppPic = document.getElementById('opponent')

Your user and opponent are class, but you use document.getElementById('user') .您的useropponent是 class,但您使用document.getElementById('user') This is some advice这是一些建议

  1. use document.getElementsByClassName('user') or document.querySelector('.user') instead改用document.getElementsByClassName('user')document.querySelector('.user')
  2. use <div id="user"></div> instead <div class="user"></div>使用<div id="user"></div>代替<div class="user"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Javascript 文件(不是脚本标签)中读取 null 的属性(读取“addEventListener”) - Cannot read properties of null (reading 'addEventListener') in a Javascript file (not script tag) html 元素在 Javascript 文件之后加载,即使<script> tag is at the bottom of the <body> tag - html elements load after Javascript file even though the <script> tag is at the bottom of the <body> tag 即使在加载身体后也无法将属性“ innerHTML”设置为null - Cannot set property 'innerHTML' of null even after body load 无法读取 null 错误的属性“innerHTML”。 我的脚本在正文的末尾,为什么我仍然收到此错误? - Cannot read property 'innerHTML' of null error. My script is at the end of the body, why am I still getting this error? 即使元素存在,也无法读取 null 的属性(读取“addEventListener”) - Cannot read properties of null (reading 'addEventListener') even though element exists 是不是放错了<script> tag after the </body> tag? - Is it wrong to place the <script> tag after the </body> tag? 尝试控制台时无法读取 null 的属性(读取“addEventListener”)。单击 btn 时记录 html 输入标签的值 - Cannot read properties of null (reading 'addEventListener') when trying to console.log the value of the html input tag when btn click 如何访问中的元素<script> inside the body tag - How to access the elements in <script> inside the body tag 如何在body标签后附加jQuery脚本? - How to append jquery script after a body tag? 打开后立即放置脚本<body>标签 - Place a script immediately after the opening <body> tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM