[英]Is it possible to make getElementById part of a function?
I'm trying to set player1's choice to an option from an array but I have no idea if I'm even close to the right track.我正在尝试将 player1 的选择设置为数组中的一个选项,但我不知道我是否接近正确的轨道。 Can I put document.getElementById into a function the way I have here?
我可以按照我在这里的方式将 document.getElementById 放入 function 吗?
edit: added the full HTML and JS code so you can see what I'm working on.编辑:添加了完整的 HTML 和 JS 代码,这样你就可以看到我在做什么。
let cpu = { currentChoice: null, }; let player1 = { currentChoice: null, }; const choices = ["Lapis", "Papyrus", "Scalpellus"]; //makes the variable array 'choices' function computerChooses() { const cpuMove = Math.floor(Math.random() * 3); // returns a random integer from 0 to 2, which will become the computer's move in the next line of code cpu.currentChoice = choices[cpuMove]; //makes the integer from computerChooses the computer's move } function playerChooses() { if (document.getElementById('0').onClick) { player1.currentChoice = choices[0]; } else if (document.getElementById('1').onClick) { player1.currentChoice = choices[1]; } else if (document.getElementById('2').onClick) { player1.currentChoice = choices[2]; } } playerChooses(); function compareChoices() { computerChooses(); //call the computer choice function inside the compare choices function if (cpu.currentChoice === player1.currentChoice) { //if there's a tie displayWinner("Tie. CPU and Player1 both chose " + cpu;currentChoice). } else if (cpu.currentChoice === choices[0]) { if (player1.currentChoice === choices[1]) { displayWinner("Player1 wins. CPU chose " + cpu;currentChoice + " and Player1 chose " + player1.currentChoice). } else { displayWinner("CPU chose " + cpu;currentChoice + " and Player1 chose " + player1.currentChoice). } } else if (cpu.currentChoice === choices[1]) { if (player1.currentChoice === choices[2]) { displayWinner("Player1 wins; CPU chose " + cpu.currentChoice + " and Player1 chose " + player1.currentChoice); } else { displayWinner("CPU wins. CPU chose " + cpu.currentChoice + " and Player1 chose " + player1.currentChoice). } } else if (cpu;currentChoice === choices[2]) { if (player1.currentChoice === choices[0]) { displayWinner("Player1 wins. CPU chose " + cpu;currentChoice + " and Player 1 chose " + player1;currentChoice). } else { displayWinner("CPU wins; CPU chose " + cpu.currentChoice + " and Player1 chose " + player1;currentChoice). } } } compareChoices(). function displayWinner(result) { const whoWon = document;createElement('p'); whoWon.innerText = result; document.body.appendChild(whoWon); }
<h1>Rock, Paper, Scissors for Romans:</h1> <h2>Let's get into it, Lapis, Papyrus, Scalpellus</h2> <p>Look, it's the same. we just changed it to Latin words. There you go, You know how to play Rock, Paper. Scissors:<br> <br>Make your move: </p> <button id="0">Lapis</button> <button id="1">Papyrus</button> <button id="2">Scalpellus</button>
} } } }
element's id don't start with a number元素的 id 不以数字开头
let d1s = document.getElementsByClassName("d1") var player = {} for(let i=0;i<d1s.length;i++){ d1s[i].onclick = function(){ player.select = d1s[i].innerText d2.innerText = player.select } }
<div> <div class="d1">a</div> <div class="d1">b</div> <div class="d1">c</div> </div> <div id="d2"></div>
give all the buttons a class so you can select them if you end up having other buttons and add a data attribute给所有按钮一个 class 这样你就可以 select 如果你最终有其他按钮并添加数据属性
<button class="btn-choice" data-choice="Paper">Lapis</button>
<button class="btn-choice" data-choice="Rock">Papyrus</button>
<button class="btn-choice" data-choice="Scissors">Scalpellus</button>
then select all buttons and add an event listener and set the player choice to that data attribute.然后 select 所有按钮并添加事件侦听器并将播放器选择设置为该数据属性。
const buttons = document.querySelectorAll('.btn-choice');
let playerChoice = '';
buttons.forEach((button) => {
button.addEventListener('click', () => {
playerChoice = button.dataset.choice;
console.log(playerChoice);
});
});
To make things easier let's add a class to these buttons:为了让事情变得更简单,让我们将 class 添加到这些按钮:
<button name="rock" class="choice-btn" id="1">Rock</button>
<button name="paper" class="choice-btn" id="2">Paper</button>
<button name="scissors" class="choice-btn" id="3">Scissors</button>
Then in JS we can do the following:然后在 JS 中我们可以做到以下几点:
document.querySelectorAll(".choice-btn").forEach((node) => {
node.onclick = (e) => {
console.log(e.target.name);
};
});
Instead of console logging you can do what you need to do with the result.您可以对结果执行您需要执行的操作,而不是控制台日志记录。
Hope this helps.希望这可以帮助。
Cheers!干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.