如何修复“无法读取null的属性'addEventListener'”错误

时间:2019-01-13 17:46:58

标签: javascript html ecmascript-6

我正在从Mozilla开发人员网络(MDN)学习javascript。我在数字猜谜游戏中遇到错误。 该错误在第46行显示“未捕获的TypeError:无法读取null的属性'addEventListener'”。

Screenshot of Error

我已经逐行检查并匹配了很多次代码,但是我无法解决问题。当我删除script.js外部文件并将代码放入标签内的index.html中时,代码部分起作用(某些功能不起作用)。这是代码。请注意,JavaScript代码和HTML位于不同的文件中。

let randomNumber = Math.floor(Math.random() * 100) + 1;

const guesses = document.querySelector('.guesses');
const lastResult = document.querySelector('.lastResult');
const loworHi = document.querySelector('.loworHi');

const guessSubmit = document.querySelector('.guessSubmit');
const guessField = document.querySelector('.guessField');

let guessCount = 1;
let resetButton;


function checkGuess() {
  let userGuess = Number(guessField.value);
  if (guessCount === 1) {
    guesses.textContent = 'Previous guesses: ';
  }

  guesses.textContent += userGuess + ' ';

  if (userGuess === randomNumber) {
    lastResult.textContent = 'Congratulations! You got it right!';
    lastResult.style.backgroundColor = 'green';
    loworHi.textContent = '';
    setGameOver();
  } else if (guessCount === 10) {
    lastResult.textContent = '!!!GAME OVER!!!';
    setGameOver();
  } else {
    lastResult.textContent = 'Wrong!';
    lastResult.style.backgroundColor = 'red';
    if (userGuess < randomNumber) {
      loworHi.textContent = 'Last guess was too low!';
    } else if (userGuess > randomNumber) {
      loworHi.textContent = 'Last guess was too high!';
    }
  }
  guessCount++;
  guessField.value = '';
  guessField.focus();

}

guessSubmit.addEventListener('click', checkGuess);

function setGameOver() {
  guessField.disabled = true;
  guessSubmit.disabled = true;
  resetButton = document.createElement('button');
  resetButton.textContent = 'Start new game';
  document.body.appendChild(resetButton);
  resetButton.addEventListener('click', resetGame);
}

function resetGame() {
  guessCount = 1;

  const resetParas = document.querySelectorAll('.resultParas p');
  for (let i = 0; i < resetParas.length; i++) {
    resetParas[i].textContent = '';
  }

  resetButton.parentNode.removeChild(resetButton);

  guessField.disabled = false;
  guessSubmit.disabled = false;
  guessField.value = '';
  guessField.focus();

  lastResult.style.backgroundColor = 'white';

  randomNumber = Math.floor(Math.random() * 100) + 1;
} 




<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="script.js"></script>
    <title>Number guessing game</title>

</head>

<body>
    <h1>Number guessing game</h1>

    <p>We have selected a random number between 1 and 100. See if you can guess it in 10 turns or fewer. 
        We'll tell you if your guess was too high or too low.</p>

    <div class="form">
        <label for="guessField">Enter a guess: </label>
        <input type="text" id="guessField" class="guessField">
        <input type="submit" value="Submit guess" class="guessSubmit">
    </div>

    <div class="resultParas">
        <p class="guesses"></p>
        <p class="lastResult"></p>
        <p class="lowOrHi"></p>
    </div>


</body>

</html> 

1 个答案:

答案 0 :(得分:1)

在加载HTML正文之前,您正在mwoffliner --mwUrl=http://192.168.56.5/YourWiki --adminEmail=foo@bar.net --verbose --redis=redis://127.0.0.1:6379 --mwWikiPath=/ --mwApiPath=api.php --localParsoid中加载Javascript文件。因此你的线

i = 2
for col in df.columns:
    df['K_{}'.format(i)] = df[col] ** i
    i += 1

没有任何内容可查找-您的类<head>的按钮尚不存在。而且由于const guessSubmit = document.querySelector('.guessSubmit'); 为null,因此它不包含任何属性,因此会出现错误。

最简单的解决方案是将guessSubmit标记放在HTML主体的底部而不是头部的底部。这样,它将在DOM完全加载后加载您的Javascript。

替代方法包括添加事件侦听器以在DOM加载后运行脚本,例如guessSubmit。在此处查看更多信息:https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded