简体   繁体   English

单击图像不显示 textContent

[英]Clicking an image does not display the textContent

I am creating a Rock, Paper, and Scissors game.我正在创建一个石头、纸和剪刀游戏。 Most of the time, when I click the rock image, the text content ( computerPicked.textContent ) will be displayed below the image;大多数情况下,当我单击岩石图像时,文本内容( computerPicked.textContent )将显示在图像下方; however, sometimes when I refresh the page and click the rock image, the text content would not appear.但是,有时当我刷新页面并单击岩石图像时,文本内容不会出现。 There were times when I had to click the rock image 3-5 times until the text content appeared below it.有时我不得不点击岩石图像 3-5 次,直到文本内容出现在它下面。

 function computerPlay() { let values = ['Rock', 'Paper', 'Scissors'], valueToUse = values[Math.floor(Math.random() * values.length)]; return valueToUse; }; const rock = document.getElementById('rock'); let playerPicked = document.getElementsByClassName('playerPicked')[0]; let computerPicked = document.getElementsByClassName('computerPicked')[0]; let result = document.getElementsByClassName('result')[0]; rock.addEventListener('click', function() { if (computerPlay() === 'Rock') { computerPicked.textContent = "Computer Pick: Rock"; } else if (computerPlay() === 'Paper') { computerPicked.textContent = "Computer Pick: Paper"; } else if (computerPlay() === 'Scissors') { computerPicked.textContent = "Computer Pick: Scissors"; } });
 <div class="wrapper"> <h1>Rock, Paper, Scissors!</h1> <h2 id="choose">Choose:</h2> </div> <div class="container"> <button> <img id="rock" src="https://static.thenounproject.com/png/477914-200.png"> </button> <button> <img src="https://static.thenounproject.com/png/477922-200.png"> </button> <button> <img src="https://static.thenounproject.com/png/477919-200.png"> </button> </div> <div class="wrapper"> <p class='playerPicked'></p> <p class='computerPicked'></p> <p class='result'></p> </div>

As mentioned in the comments, you should call computerPlay() only once per click and then save the returned value to a variable.正如评论中提到的,您应该每次点击只调用一次computerPlay() ,然后将返回的值保存到一个变量中。 If you don't, computerPlay() will return a completely new, random value each time it is called (the value will keep changing making it really hard to compare against).如果不这样做,则每次调用computerPlay()都会返回一个全新的随机值(该值会不断变化,因此很难进行比较)。 eg例如

const compMove = computerPlay();

Note that here we are assigning the returned value of calling the function to the variable compMove , and not a reference to the function itself.请注意,这里我们将调用函数返回值分配给变量compMove ,而不是对函数本身的引用。


The other helpful point of feedback in the comments is that you can use document.querySelector(.myClass) instead of document.getElementsByClassName('myClass')[0] to help shorten your code.评论中另一个有用的反馈点是您可以使用document.querySelector(.myClass)而不是document.getElementsByClassName('myClass')[0]来帮助缩短代码。


Also, instead of attaching click handlers one by one, you can use document.querySelectorAll to attach a handler to all the buttons at once with forEach .此外,您可以使用document.querySelectorAll使用forEach一次将处理程序附加到所有按钮,而不是一个一个地附加点击处理程序。 While iterating through the collection of buttons, you can extract data about the move from a data attribute.在迭代按钮集合时,您可以从数据属性中提取有关移动的数据。 eg例如

  • <button data-move="rock">
  • const playerMove = e.currentTarget.dataset.move

 function computerPlay() { let values = ['Rock', 'Paper', 'Scissors'], valueToUse = values[Math.floor(Math.random() * values.length)]; return valueToUse; }; const moves = document.querySelectorAll('button'); let playerPicked = document.querySelector('.playerPicked'); let computerPicked = document.querySelector('.computerPicked'); let result = document.querySelector('.result'); moves.forEach(move => move.addEventListener('click', function(e) { const compMove = computerPlay(); const playerMove = e.currentTarget.dataset.move computerPicked.textContent = `Computer Picked: ${compMove}`; playerPicked.textContent = `You Picked: ${playerMove}`; }));
 <div class="wrapper"> <h1>Rock, Paper, Scissors!</h1> <h2 id="choose">Choose:</h2> </div> <div class="container"> <button data-move="Rock"> <img src="https://static.thenounproject.com/png/477914-200.png"> </button> <button data-move="Paper"> <img src="https://static.thenounproject.com/png/477922-200.png"> </button> <button data-move="Scissors"> <img src="https://static.thenounproject.com/png/477919-200.png"> </button> </div> <div class="wrapper"> <p class='playerPicked'></p> <p class='computerPicked'></p> <p class='result'></p> </div>

I think your idea was this ...我想你的想法是这样的......

You put to draw and display the draw and not the choice of the person.你把画画和显示画,而不是人的选择。

I fixed the possibility of the computer choosing the same as the player.我修复了计算机选择与播放器相同的可能性。

html html

 function listen(obj) { obj.addEventListener('click', function() { let computerPicked = document.querySelector("h3"); let playerPicked = document.querySelector(".player"); playerPicked.textContent = `Player Pick: ${obj.id}`; computerPicked.textContent = `Computer Pick: ${computerPlay(obj.id)}`; }); } function computerPlay(except) { let values; switch (except) { case 'Rock': values = ['Paper', 'Scissors']; break; case 'Paper': values = ['Rock', 'Scissors'] break; case 'Scissors': values = ['Rock', 'Paper'] break; } valueToUse = values[Math.floor(Math.random() * values.length)]; return valueToUse; }; const rock = document.querySelector('#rock'); const Paper = document.querySelector('#Paper'); const Scissors = document.querySelector('#Scissors'); let playerPicked = document.getElementsByClassName('playerPicked')[0]; let computerPicked = document.getElementsByClassName('computerPicked')[0]; let result = document.getElementsByClassName('result')[0]; listen(Rock); listen(Paper); listen(Scissors);
 <div class="wrapper"> <h1>Rock, Paper, Scissors!</h1> <h2 id="choose">Choose:</h2> <h3 class="computer"> </h3> <h3 class="player"> </h3> <h3 class="whoWins"> </h3> </div> <div class="container"> <button> <img id="Rock" src="https://static.thenounproject.com/png/477914-200.png"> </button> <button> <img id="Paper" src="https://static.thenounproject.com/png/477922-200.png"> </button> <button> <img id='Scissors' src="https://static.thenounproject.com/png/477919-200.png"> </button> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM