Uncaught TypeError:单击按钮[closed]时无法读取null的属性'addEventListener'

问题描述 投票:-1回答:1
当我偶然发现

Uncaught TypeError:Can not read property'addEventListener'of null]时,我按照freecodecamp.org的课程尝试创建基本的Rock,Paper Scissors游戏。 起初,我以为是因为DOM加载不正确,因为我将其放在head标签而不是body标签的底部,但是当我更改它时,它只能播放一次,然后它发送给我同样的错误。

我不知道如何解决。我共享了html和JS。

HTML:

<!DOCTYPE html> <html> <head> <title>Rock, Paper, Scissors</title> <link rel="stylesheet" type="text/css" href="styles/main.css"> <script src="https://kit.fontawesome.com/8192e1e51b.js" crossorigin="anonymous"></script> <link href="https://fonts.googleapis.com/css2?family=Rubik:wght@300;400&display=swap" rel="stylesheet"> </head> <body> <header class="header"> <h1>Rock Paper Scissors</h1> </header> <div class="score-board"> <div id="user-badge" class="badge">User</div> <div id="robot-badge" class="badge">Robot</div> <span class="score-numbers">0</span>:<span class="score-numbers">0</span> </div> <div class="result"><p>Paper covers Rock.<br>You Win!</p></div> <div class="choices"> <div id="rock" class="choice"><i class="far fa-hand-rock"></i></div> <div id="paper" class="choice"><i class="far fa-hand-spock"></i></div> <div id="scissors" class="choice"><i class="far fa-hand-scissors"></i></div> </div> <p class="move-message">Make your move</p> </div> <script type="text/javascript" src="scripts/main.js"></script> </body> </html>

JavaScript:

const userScore = 0; const robotScore = 0; const userScore_span = document.getElementById("user-badge"); const robotScore_span = document.getElementById("robot-badge"); const scoreBoard_div = document.querySelector(".score-board"); const result_div = document.querySelector(".result"); const rock_div = document.getElementById("r"); const paper_div = document.getElementById("p"); const scissors_div = document.getElementById("s"); rock_div.addEventListener("click", function(){ alert("rock"); game("r"); }) paper_div.addEventListener("click", function(){ alert("paper"); game("p"); }) scissors_div.addEventListener("click", function(){ alert("scissors"); game("s"); })

当我偶然发现Uncaught TypeError:无法读取null的属性'addEventListener'时,我尝试按照freecodecamp.org课程创建一个基本的Rock,Paper Scissors游戏。首先我...
javascript html css dom
1个回答
-1
投票
在您共享的HTML中
© www.soinside.com 2019 - 2024. All rights reserved.