简体   繁体   English

未捕获的类型错误:无法读取未定义的属性(读取“classList”)

[英]Uncaught TypeError: Cannot read properties of undefined (reading 'classList')

not quite sure why I'm getting an error in the console, the game is working correctly and I have fiddled around for hours trying to find a solution.不太清楚为什么我在控制台中出现错误,游戏运行正常,我已经摆弄了几个小时试图找到解决方案。 I have a few error codes, the first one is script.js:28 Uncaught TypeError: Cannot read properties of undefined (reading 'classList') at randomSquare (script.js:28:17) and the second error code is: script.js:80 Uncaught TypeError: Cannot set properties of null (setting 'textContent') at countDown (script.js:80:24) I think this could be due to having an animation of the game on my homepage but when I removed it, the error codes still appear.我有一些错误代码,第一个是 script.js:28 Uncaught TypeError: Cannot read properties of undefined (reading 'classList') at randomSquare (script.js:28:17),第二个错误代码是:script.js:28 Uncaught TypeError: Cannot read properties of undefined (reading 'classList') at randomSquare (script.js:28:17) js:80 Uncaught TypeError: Cannot set properties of null (setting 'textContent') at countDown (script.js:80:24)错误代码仍然出现。 I'm fairly new to coding and completely lost right now, any help appreciated, thanks我对编码还很陌生,现在完全迷路了,感谢您的帮助,谢谢

const squares = document.querySelectorAll('.square');
const mole = document.querySelector('.mole-image');
const timeLeft = document.querySelector('#time-left');
const scoreDisplay = document.querySelector('#score');
const cursor = document.querySelector(".cursor-mallet img");

let molePosition;
let score = 0;
let currentTime = 20;
let timerId = null;


let successfulWhack = () => new Audio("assets/audio/hit-sound.flac").play();
let playSound = () => new Audio("assets/audio/hammer-whack.wav").play();
let audio = document.getElementById('audio');
let playPauseBtn = document.getElementById('playPauseBtn');
let count = 0;


function randomSquare() {
squares.forEach(square => {
  square.classList.remove('mole-image');
});

let randomSquare = squares[Math.floor(Math.random() * 12)];
   randomSquare.classList.add('mole-image');


   molePosition = randomSquare.id;
}
  squares.forEach(square => {
  square.addEventListener('mousedown', () => {
  if (square.id == molePosition) {
    successfulWhack();
    score++;
    scoreDisplay.textContent = score;
    molePosition = null;
  }
});
});

function moveMole() {
 moleTimerId = setInterval(randomSquare, 800);
}

moveMole();

function countDown() {
  currentTime--;
  timeLeft.textContent = currentTime;
  
if (currentTime ==  0) {
    clearInterval(countDownTimerId);
    clearInterval(moleTimerId);
    window.alert('GAME OVER! Your final score is ' + score);
    location.reload();
    playSound();
    }
  }
let countDownTimerId = setInterval(countDown, 1000);

It looks like the randomSquare variable does not have a classList property.看起来 randomSquare 变量没有 classList 属性。 It looks like timeLeft is null.看起来 timeLeft 是 null。

You should be able to validate this by logging the values of each before you call them.您应该能够通过在调用它们之前记录每个值来验证这一点。 For example, you can put this logging in place:例如,您可以将此日志记录到位:

// in randomSquare()
let randomSquare = squares[Math.floor(Math.random() * 12)];
console.log('randomSquare: ' + JSON.stringify(randomSquare));
console.log('randomSquare.classList: ' + JSON.stringify(randomSquare.classList));

// in countDown()
currentTime--;
console.log('timeLeft: ' + JSON.stringify(timeLeft));

// This will probably throw an error and not print
console.log('timeLeft.textContent: ' + JSON.stringify(timeLeft.textContent));

edit: Try logging with and without JSON.stringify.编辑:尝试使用和不使用 JSON.stringify 进行日志记录。

I would also change the randomSquare variable inside the function randomSquare() to be any different name so that you know you're not calling the function when you mean to be calling the variable.我还将 function randomSquare() 中的 randomSquare 变量更改为任何不同的名称,以便您知道当您打算调用该变量时您没有调用 function。

暂无
暂无

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

相关问题 为什么 classList 会返回错误:Uncaught TypeError: Cannot read properties of undefined (reading 'classList')? - Why does classList return an error: Uncaught TypeError: Cannot read properties of undefined (reading 'classList')? 未捕获的类型错误:无法读取 Javascript 中未定义(读取“classList”)的属性“值” - Uncaught TypeError: Cannot read property 'value' of undefined (reading 'classList') in Javascript 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的TypeError:无法读取未定义的属性'classList' - Uncaught TypeError: Cannot read property 'classList' of undefined TypeError:无法读取 null 的属性(读取“classList”) - TypeError: Cannot read properties of null (reading 'classList') 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM