javascript - 单击按钮元素时如何更新属性?

标签 javascript html css

我在这里遇到一个问题,我正在尝试创建这个石头、剪刀、布游戏,但在供用户选择的按钮选择方面遇到问题。我没有运气获得更新playerTurn.currentPick属性的代码来更新玩家在按钮单击时选择的内容。我创建了一个名为 choiceBtn 的函数,它应该用用户选择的内容更新属性。正如你所知,我是编码新手,但任何帮助将不胜感激。下面是我的 JS 代码,如果有帮助的话我也会附上 HTML 代码。

let playerTurn = {
  currentPick: null
  
};
let cpuTurn = {
  currentPick: null
};


const playerChoice = ["Lapis", "Papyrus", "Scalpellus"];
playerTurn.currentPick = choiceBtn();

function computerChoice() {
  const randomChoice = Math.floor(Math.random() * playerChoice.length);
cpuTurn.currentPick = playerChoice[randomChoice];

return cpuTurn.currentPick;
                       }
console.log(computerChoice());

function compareChoices(){
  computerChoice();
  if(playerTurn.currentPick === cpuTurn.currentPick){
    resultScreen("It's a tie. Both the player and cpu chose " + playerTurn.currentPick);
  
} else if(playerTurn.currentPick === playerChoice[0]){
} if(cpuTurn.currentPick === playerChoice[2]){
  resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
} else if(playerTurn.currentPick === playerChoice[1]){
  if(cpuTurn.currentPick === playerChoice[0]){
    resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
  }
} else if(playerTurn.currentPick === playerChoice[2]){
  if(cpuTurn.currentPick === playerChoice[1]){
    resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
  }
} else if(cpuTurn.currentPick === playerChoice[0]){
  if(playerTurn.currentPick === playerChoice[2]){
    resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
  }
}
  else if(cpuTurn.currentPick === playerChoice[1]){
    if(playerTurn.currentPick === playerChoice[0]){
      resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
    }
  }
  else if(cpuTurn.currentPick === playerChoice[2]){
    if(playerTurn.currentPick === playerChoice[1]){
       resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
    }
  }
  
}
function resultScreen(display){
  const displayText = document.createElement('p');
  displayText.innerText = display;
  document.body.appendChild(displayText);
}
function choiceBtn(){
 var lapisBtn = document.getElementById("lapis").onclick = function(){
  playerTurn.currentPick = playerChoice[0];
 }
  var papyrusBtn =  document.getElementById("papyrus").onclick = function(){
   papyrusBtn = playerChoice[1];
 }
  var scalpellusBtn = document.getElementById("scalpellus").onclick = function(){
   scalpellusBtn = playerChoice[2];
 }
}
compareChoices();
<!--HTML CODE-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Lapis, Papyrus, Scalpelus</title>
</head>
<body>
  Welcome to the game of Lapis, Papyrus, and Scalpelus!
  <h2>Make your Choice Below</h2>
  <button id="lapis">Lapis</button>
  <button id="papyrus">Papyrus</button>
  <button id="scalpellus">Scalpellus</button>
</body>
</html>

最佳答案

你走在正确的道路上。定义点击事件时存在几个问题。我从它们所在的函数中删除了它们,以便可以在运行开始时定义它们。将玩家选择移至当前选择变量也存在问题。你的布和剪刀正在被移动到变量名称。

该片段显示 onClick 事件正在运行,但在检查游戏结果方面似乎确实需要进一步的工作。谁赢并不总是正确的

let playerTurn = {
  currentPick: null

};
let cpuTurn = {
  currentPick: null
};


const playerChoice = ["Lapis", "Papyrus", "Scalpellus"];
// playerTurn.currentPick = choiceBtn();

function computerChoice() {
  const randomChoice = Math.floor(Math.random() * playerChoice.length);
  cpuTurn.currentPick = playerChoice[randomChoice];

  return cpuTurn.currentPick;
}
console.log(computerChoice());

function compareChoices() {
  computerChoice();
  if (playerTurn.currentPick === cpuTurn.currentPick) {
    resultScreen("It's a tie. Both the player and cpu chose " + playerTurn.currentPick);

  } else if (playerTurn.currentPick === playerChoice[0]) {}
  if (cpuTurn.currentPick === playerChoice[2]) {
    resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
  } else if (playerTurn.currentPick === playerChoice[1]) {
    if (cpuTurn.currentPick === playerChoice[0]) {
      resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
    }
  } else if (playerTurn.currentPick === playerChoice[2]) {
    if (cpuTurn.currentPick === playerChoice[1]) {
      resultScreen("The player wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
    }
  } else if (cpuTurn.currentPick === playerChoice[0]) {
    if (playerTurn.currentPick === playerChoice[2]) {
      resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
    }
  } else if (cpuTurn.currentPick === playerChoice[1]) {
    if (playerTurn.currentPick === playerChoice[0]) {
      resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
    }
  } else if (cpuTurn.currentPick === playerChoice[2]) {
    if (playerTurn.currentPick === playerChoice[1]) {
      resultScreen("The cpu wins. The player chose " + playerTurn.currentPick + " and the cpu chose " + cpuTurn.currentPick + ".");
    }
  }

}

function resultScreen(display) {
  const displayText = document.createElement('p');
  displayText.innerText = display;
  document.body.appendChild(displayText);
}

var lapisBtn = document.getElementById("lapis").onclick = function() {
  playerTurn.currentPick = playerChoice[0];
  compareChoices();
}
var papyrusBtn = document.getElementById("papyrus").onclick = function() {
  playerTurn.currentPick = playerChoice[1];
  compareChoices();
}
var scalpellusBtn = document.getElementById("scalpellus").onclick = function() {
  playerTurn.currentPick = playerChoice[2];
  compareChoices();
}
Welcome to the game of Lapis, Papyrus, and Scalpelus!
<h2>Make your Choice Below</h2>
<button id="lapis">Lapis</button>
<button id="papyrus">Papyrus</button>
<button id="scalpellus">Scalpellus</button>

关于javascript - 单击按钮元素时如何更新属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65676643/

相关文章:

javascript - 单击按钮时在两个功能之间切换。

html - 在这里,我希望我的底部有 100% 的高度,我不想超过我在 “.main” 类中固定的 400px 高度

javascript - 获取与javascript中特定路径匹配的所有href

javascript - 如何使用按钮调用 Javascript

javascript - 需要绘制一 strip 有平滑 Angular 的褪色线

javascript - foreach 和 JavaScript

javascript - 将 div 背景设置为相机拍摄的照片

css - 使用 div 作为其中 float 元素的视口(viewport)

javascript - 在下拉菜单上滚动时如何防止正文滚动。?

html - 为什么我的页面不能在 Internet Explorer 中正确呈现?