使用 html & 重新启动按钮js
如何在此游戏上添加重新启动按钮,以便每次用户单击该按钮时都会重新启动游戏。尝试了几种方法来做到这一点,但我认为我很愚蠢,哈哈,只是无法弄清楚。 有人知道该怎么做吗?
如何在此游戏上添加重新启动按钮,以便每次用户单击该按钮时都会重新启动游戏。尝试了几种方法来做到这一点,但我认为我很愚蠢,哈哈,只是无法弄清楚。 有人知道该怎么做吗?
html
----------
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Rock Paper Scissors Tutorial</title>
</head>
<body>
<h2>Computer Choice: <span id="computer-choice"></span></h2>
<h2>Your Choice: <span id="user-choice"></span></h2>
<h2>Result: <span id="result"></span></h2>
<button id="rock">rock</button>
<button id="paper">paper</button>
<button id="scissors">scissors</button>
<div class="restart">Re-Start</div>
<script src="app.js" charset="utf-8"></script>
</body>
</html>
--------
js file
---------
const computerChoiceDisplay = document.getElementById('computer-choice')
const userChoiceDisplay = document.getElementById('user-choice')
const resultDisplay = document.getElementById('result')
const possibleChoices = document.querySelectorAll('button')
let userChoice
let computerChoice
let result
possibleChoices.forEach(possibleChoice => possibleChoice.addEventListener('click', (e) => {
userChoice = e.target.id
userChoiceDisplay.innerHTML = userChoice
generateComputerChoice()
getResult()
}))
function generateComputerChoice() {
const randomNumber = Math.floor(Math.random() * 3) + 1 // or you can use possibleChoices.length
if (randomNumber === 1) {
computerChoice = 'rock'
}
if (randomNumber === 2) {
computerChoice = 'scissors'
}
if (randomNumber === 3) {
computerChoice = 'paper'
}
computerChoiceDisplay.innerHTML = computerChoice
}
function getResult() {
if (computerChoice === userChoice) {
result = 'its a draw!'
}
if (computerChoice === 'rock' && userChoice === "paper") {
result = 'you win!'
}
if (computerChoice === 'rock' && userChoice === "scissors") {
result = 'you lost!'
}
if (computerChoice === 'paper' && userChoice === "scissors") {
result = 'you win!'
}
if (computerChoice === 'paper' && userChoice === "rock") {
result = 'you lose!'
}
if (computerChoice === 'scissors' && userChoice === "rock") {
result = 'you win!'
}
if (computerChoice === 'scissors' && userChoice === "paper") {
result = 'you lose!'
}
resultDisplay.innerHTML = result
}
document.getElementById("reset").onclick = function() {
document.getElementById("number").innerHTML = "";
};
How do you add a restart button on this game so that every time user click on the button it restart the game. Tried few ways to do it but i think i am dumb lol, just cant figure it out.
Anyone knows how to do that ?
How do you add a restart button on this game so that every time user click on the button it restart the game. Tried few ways to do it but i think i am dumb lol, just cant figure it out.
Anyone knows how to do that ?
html
----------
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Rock Paper Scissors Tutorial</title>
</head>
<body>
<h2>Computer Choice: <span id="computer-choice"></span></h2>
<h2>Your Choice: <span id="user-choice"></span></h2>
<h2>Result: <span id="result"></span></h2>
<button id="rock">rock</button>
<button id="paper">paper</button>
<button id="scissors">scissors</button>
<div class="restart">Re-Start</div>
<script src="app.js" charset="utf-8"></script>
</body>
</html>
--------
js file
---------
const computerChoiceDisplay = document.getElementById('computer-choice')
const userChoiceDisplay = document.getElementById('user-choice')
const resultDisplay = document.getElementById('result')
const possibleChoices = document.querySelectorAll('button')
let userChoice
let computerChoice
let result
possibleChoices.forEach(possibleChoice => possibleChoice.addEventListener('click', (e) => {
userChoice = e.target.id
userChoiceDisplay.innerHTML = userChoice
generateComputerChoice()
getResult()
}))
function generateComputerChoice() {
const randomNumber = Math.floor(Math.random() * 3) + 1 // or you can use possibleChoices.length
if (randomNumber === 1) {
computerChoice = 'rock'
}
if (randomNumber === 2) {
computerChoice = 'scissors'
}
if (randomNumber === 3) {
computerChoice = 'paper'
}
computerChoiceDisplay.innerHTML = computerChoice
}
function getResult() {
if (computerChoice === userChoice) {
result = 'its a draw!'
}
if (computerChoice === 'rock' && userChoice === "paper") {
result = 'you win!'
}
if (computerChoice === 'rock' && userChoice === "scissors") {
result = 'you lost!'
}
if (computerChoice === 'paper' && userChoice === "scissors") {
result = 'you win!'
}
if (computerChoice === 'paper' && userChoice === "rock") {
result = 'you lose!'
}
if (computerChoice === 'scissors' && userChoice === "rock") {
result = 'you win!'
}
if (computerChoice === 'scissors' && userChoice === "paper") {
result = 'you lose!'
}
resultDisplay.innerHTML = result
}
document.getElementById("reset").onclick = function() {
document.getElementById("number").innerHTML = "";
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尽可能少地编辑了你的代码:
所以基本上,如果你想单击文本“重新开始”并让它“重置游戏”,你必须在三个地方删除文本:计算机的选择,你的选择,以及结果。
您为其编写的函数不会改变其中任何一个。相反,它会查找 id 为“number”的 DOM 元素,而该元素似乎根本不存在。
您想要更改的 DOM 元素的 ID 为“computer-choice”、“user-choice”和“result”。
此外,您正在尝试将重置功能的点击事件添加到 id 为“reset”的元素。但它不存在。您可以通过将该 ID 添加到您似乎愿意在这种情况下使用的文本来更改它:
而不是
如果您想要一个“重新启动按钮”,那么您可以使用文本“Re- Start”添加一个类,使其在视觉上更像一个按钮,或者只是使用
I edited as little as possible of your code :
So basically, if you want to click the text "Re-Start" and having it "reset the game", you have to remove the text at three places : the computer's choice, your choice, and the result.
The function your wrote for it doesn't change any of those. Instead, it looks for a DOM element with the id "number", which doesn't seem to even exists.
The DOM elements you want to change have IDs "computer-choice", "user-choice" and "result".
Beside, you're trying to add a click event toward your reset function to an element with the id "reset". But it doesn't exists. You can change that by adding that ID to the text which you seem to be willing to use in that case :
instead of
And if you want to have a "restart button", then you may either edit the div with the text "Re-Start" to add a class which makes it more of a button visually speaking, or just use