如何修复:TypeError:'null'不是对象(评估'event.relatedTarget.parentNode')

时间:2014-03-10 21:01:11

标签: javascript

我上传了一个我正在处理的网站后出现错误。

TypeError:'null'不是对象(评估'event.relatedTarget.parentNode')

关于如何解决这个/我应该用它替换它的任何想法?

2 个答案:

答案 0 :(得分:5)

如果没有任何其他上下文,如果event.relatedTarget.parentNode给出了此错误,则表示eventevent.relatedTarget为空。

所以:

if (event && event.relatedTarget && event.relatedTarget.parentNode) {
    // your code here
}

现在您应该如何处理eventevent.relatedTarget为空的情况?我不知道,因为你没有提供足够的信息让任何人知道你在做什么。

您可能需要做的是弄清楚为什么它有时(或者可能一直)为空。

答案 1 :(得分:0)

我遇到了同样的问题。我试图在https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/A_first_splash上创建数字猜谜游戏。 尽管我准确键入了代码,但我一直收到TypeError:null至少不是我的两个代码的对象消息。当我以为清除它们时,我会得到另一个TypeError:Number不是函数,并且尾随零添加到我的猜测答案中。我什至会在“字段输入”焦点guessField.focus()上遇到错误。但是经过深入研究,我不仅读到了这似乎是Safari浏览器的常见错误,而且我终于弄清楚了。

我收到的所有错误消息都在功能checkGuess()中。即使我在函数之前定义了变量,也并未全部定义或识别它们。最大的问题是围绕输入字段。疯狂的是,当我第一次创建它时,它运行良好,而在单击几下后,它并没有运行。

我所做的是将变量重新定义为随机数,将其从全局变量中删除,并移至词法环境中。与函数直接关联的更好定义。游戏再次运行。 这是原始代码:

<body>

    <div class="game">
        <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>

        <label for="guessField">
            Enter a guess: </label>
        <input type="text" id="guessField" class="guessField" tabindex="1">
        <input type="submit" value="Submit Guess" class="guessSubmit" tabeindex="2" onclick="checkGuess()">

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

    </div>

</body>

<script>
var randomNumber = Math.floor(Math.random() * 100) + 1;

var guesses = document.querySelector('.guesses');
var lastResult = document.querySelector('.lastResult');
var lowOrHi = document.querySelector('.lowOrHi');

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

var guessCount = 1;
var resetButton;
guessField.focus();


function checkGuess() {
  var userGuess = randomNumber(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;

  var resetParas = document.querySelectorAll('.resultParas p');
  for (var 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;
}
</script>

这就是我将checkGuest函数更改为:

var guesses = document.querySelector('.guesses');
var lastResult = document.querySelector('.lastResult');
var lowOrHi = document.querySelector('.lowOrHi');

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

var guessCount = 1;
var resetButton;
guessField.focus();


function checkGuess() {
    var userGuess = guessField.value;
    guessField.value = Number;
    Number = Math.floor(Math.random() * 100) + 1;
    if(guessCount === 1) {
    guesses.textContent = 'Previous guesses: ';
    }
    guesses.textContent += userGuess + '';

    if(userGuess === Number) {
        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 < Number) {
            lowOrHi.textContent = 'Last guess was too low!';
        } else if(userGuess > Number) {
            lowOrHi.textContent = 'Last guess was too high!';
        }
    }

    guessCount++;
    guessField.value = '';
    guessField.focus();

guessSubmit.addEventListener('click', checkGuess);
}