所以我做了这个小东西因为我对编程很新,但是当我在Chrome中打开它时,我能够输入输入但是没有任何反应。有谁知道如何修复此代码? 提前谢谢!
<!DOCTYPE html>
<html>
<head>
<title>Number Guessing</title>
</head>
<body>
<b id="bold">Guess:</b> <input type="text" id="guess">
<input type="submit" value="GO!">
<script>
function startGame() {
function getRandomNumber(low, high) {
var number = Math.floor(Math.random() * (high - low +1)) + low;
return number;
}
var number = getRandomNumber(1,10);
var guess = document.getElementById("guess");
for (var i=0;i=0) {
if (guess>number) {
guess = document.getElementById("guess");
document.getElementById("bold").innerHTML = "You're too high, try lower!";
}
if (guess<number) {
guess = document.getElementById("guess");
document.getElementById("bold").innerHTML = "You're too low, try higher!";
}
if (guess==number) {
alert("You're correct, the number is "+number+"!!!");
alert("Thanks for playing my game and have a good day!");
}
}
}
startGame();
</script>
</body>
</html>
答案 0 :(得分:0)
从语法错误开始,您遇到了很多问题。
您有一个submit
按钮,但没有form
要提交。你真的需要一个button
。但是,即使这样,你也必须为它设置一个click
事件处理程序。
然后,您的循环未正确配置。
您也无法正确访问用户在文本框中输入的数据 - 您需要获取该元素的value
。
您的if
语句应为else if
。
b
元素不应仅用于演示。 HTML是一种语义&#34;语言,意味着您使用标记来描述元素的含义(不是表示)。对于样式使用CSS。
有关详细信息,请参阅下面的评论。
/* CSS is for presentation, not HTML */
#bold { font-weight:bold; }
&#13;
<!DOCTYPE html>
<html>
<head>
<title>Number Guessing</title>
</head>
<body>
<!-- Don't use HTML for styling, use it for semantics. -->
<span id="bold">Guess:</span> <input type="text" id="guess">
<!-- You need a <form> if you have a submit button. For this, you just want a button. -->
<input type="button" value="GO!" id="go">
<script>
function startGame() {
function getRandomNumber(low, high) {
var number = Math.floor(Math.random() * (high - low + 1)) + low;
return number;
}
var number = getRandomNumber(1,10);
var guess = document.getElementById("guess");
// Get a reference to the output area just once
var output = document.getElementById("bold");
// Give the user 3 tries. Your loop wasn't configured properly.
for (var i=0; i < 3; i++) {
// You want to access the data in the textbox. That's the value
// Also, if the first condition isn't true, try the next and so on.
// This is done with else if branches
if (guess.value > number) {
output.textContent = "You're too high, try lower!";
} else if (guess.value < number) {
output.textContent = "You're too low, try higher!";
} else if (guess.value == number) {
alert("You're correct, the number is "+number+"!!!");
alert("Thanks for playing my game and have a good day!");
break; // Get out of the loop because the game is over.
}
}
}
// Set it up so that clicks on the button run the function
document.getElementById("go").addEventListener("click", startGame);
</script>
</body>
</html>
&#13;
答案 1 :(得分:0)
你有一些错误:
这不起作用,它不会循环。实际上,你为什么要循环?
for (var i=0;i=0) {
这将运行一次该函数,这意味着当用户写入它不会被检查的值时
startGame();
按钮没有做任何事情,也有提交,你没有任何形式:
input type="submit" value="GO!">
在每个if上,条件是独占的,使用if/else
下面是一个工作代码:
<!DOCTYPE html>
<html>
<head>
<title>Number Guessing</title>
</head>
<body>
<b id="bold">Guess:</b> <input type="text" id="guess">
<input value="GO!" onclick="checkGuess()">
<script>
var number = 0;
function startGame() {
function getRandomNumber(low, high) {
var number = Math.floor(Math.random() * (high - low + 1)) + low;
return number;
}
number = getRandomNumber(1, 10);
}
function checkGuess() {
var guess = document.getElementById("guess").value;
if (guess > number) {
guess = document.getElementById("guess");
document.getElementById("bold").innerHTML = "You're too high, try lower!";
} else if (guess < number) {
guess = document.getElementById("guess");
document.getElementById("bold").innerHTML = "You're too low, try higher!";
} else if (guess == number) {
alert("You're correct, the number is " + number + "!!!");
alert("Thanks for playing my game and have a good day!");
}
}
startGame();
</script>
</body>
</html>
&#13;
答案 2 :(得分:-1)
虽然我不知道你的程序做了什么。
时出现语法错误for (var i=0;i=0) {
并且您还应该将事件绑定到该按钮而不是提交。