我正在尝试用 JavaScript 创建一个猜数字游戏,除了用户选择的数字小于“mysterNumber”的值之外,该游戏正在运行。
控制台中出现的错误是:
scripts.js:28 Uncaught TypeError: Cannot set property 'innerHTML' of null(…)
playGame @ scripts.js:28
clickHandler @ scripts.js:17
这是 HTML
// Game variables
var mysteryNumber = 50;
var playerGuess = 0;
// The input and output fields
var input = document.querySelector("#input");
var ouput = document.querySelector("#ouput");
// The button
var button = document.querySelector("button");
button.style.cursor = "pointer";
button.addEventListener("click", clickHandler, false);
function clickHandler(){
playGame();
}
function playGame(){
playersGuess = parseInt(input.value);
if(playersGuess > mysteryNumber){
output.innerHTML = "That's too high."
}else if(playersGuess < mysteryNumber){
ouput.innerHTML = "That's too low."
}else if(playersGuess === mysteryNumber){
output.innerHTML = "You got it!"
}
}
<p id="output">I am thinking of a number between 0 and 99.</p>
<input id ="input" type="text" placeholder="Enter your guess...">
<button>guess</button>
最佳答案
您的代码中有几个拼写错误。确保您始终使用“输出”而不是“输出”。
关于JavaScript:创建一个简单的猜数字游戏并出现未捕获的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41640123/