gpt4 book ai didi

javascript - 在不使用警报的情况下显示消息

转载 作者:行者123 更新时间:2023-12-03 11:50:36 24 4
gpt4 key购买 nike

我正在开发“猜猜我的数字”游戏,但遇到了问题。我的游戏应该选择 1 到 10 之间的随机整数,并允许用户猜测,直到猜出正确的数字。每次猜测后,我应该显示一条消息,告诉他们的猜测是否太高、太低、正确,或者他们之前是否猜过该数字。我通过使用警报来显示用户是否太高、太低、正确等来让游戏正常运行(除了显示之前猜测的数字数组)。

if (guess == this.num) {
alert("Correct! It took you " + turns " tries to guess my number.");
...
}

但是,回顾一下说明,我发现我们不应该使用警报或任何其他类型的弹出窗口。所以我需要弄清楚如何在屏幕上而不是在警报框中显示这些消息。

以下是我在不使用警报的情况下尝试执行此操作的方法,但现在游戏根本无法运行(当我单击任一按钮时没有任何反应):

<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>Guess My Number</title>

<script type="text/javascript">
var tries = [];
var game = {
num : 0,
turns : 1,
reset : function() {
this.turns = 1;
this.newNum();
},
newNum : function() {
this.num = parseInt(Math.random() * 10) +1;
},
guessNumber : function(guess) {
try {
guess = parseInt(guess);
}
catch(e) {
alert("Enter a guess!");
this.turns++;
return false;
}

if (guess == this.num) {
document.getElementById("result").value = "Correct! It took you " + this.turns + " tries to guess my number.");
alert("Correct! It took you " + this.turns + " turns to guess my number.");
tries.push(guess);
document.querySelector("#tries").textContent = tries.join(', ');
document.getElementById("guess").value = " ";
return true;
}
else if(guess > this.num) {
document.getElementById("result").value = "Your guess is too high. Try again.";
alert("Your guess is too high. Try again.");
document.querySelector("#tries").textContent = tries.join(', ');
this.turns++;
document.getElementById("guess").value = " ";
return false;
}

else if(tries.indexOf(guess) != -1) {
document.getElementById("result").value = "You have already guessed this number. Try again.";
this.turns++;
document.getElementById("guess").value = " ";
return false;
}
else {
document.getElementById("result").value = "Your guess is too low. Try again.";
document.querySelector("#tries").textContent = tries.join(', ');
tries.push(guess);
this.turns++;
document.getElementById("guess").value = " ";
return false;
}
}
};

function guessNumber() {
var guess = document.getElementById("guess").value;
game.guessNumber(guess);
document.getElementById("guess").value = " ";
}

function resetGame() {
game.reset();
document.getElementById("guess").value = " ";
}

resetGame();
</script>
</head>

<body>
<h1>Would You Like To Play A Game?</h1>
<h2>Thank you for checking out my game. Good luck!</h2>
<h3>Created by Beth Tanner</h3>
<h2>Directions:</h2>
<p>
The game is very simple. I am thinking of a number between 1
and 10. It is your job to guess that number. If you do not guess
correctly on your first attempt, don't worry, you can keep guessing
until you guess the correct number.
</p>
<p>
Your Guess: <input type="text" id="guess" size="10" />
<br />
<input type="button" value="Sumbit Guess" onclick="guessNumber()" />
<input type="reset" value="Reset Game" onclick="resetGame()"/>
</p>
<h3>How Did You Do?:</h3>
<p>
<input type="hidden" id="result" size="20" />
</p>
<h3>Here Are Your Guesses So Far:</h3
</body>
</html>

有什么简单的方法可以做到这一点吗?我希望这些消息显示在标题“你做得怎么样?:”下方的隐藏文本中,如果这样可以更清晰一些。

这是我用最新代码制作的jsfiddle,http://jsfiddle.net/3p3f86fj/6/

最佳答案

您应该在正文中添加一个具有唯一 ID 的 div,例如 <div id='result'></div>然后在你的 JS 中,通过 ID 获取文档来引用它,你的函数应该将 div 的文本更改为你需要的任何内容。如果您想抽象出一些逻辑,请创建一个函数来更改名称。

关于javascript - 在不使用警报的情况下显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25857919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com