繁体   English   中英

单击按钮时如何更改常量值?

[英]How do I change a const value when a button is clicked?

有人可以帮我用可以帮助我根据单击的按钮更改 player.choice 值的内容替换 console.log 行吗?

 const player = { currentChoice: null } const computer = { currentChoice: null } const choices = ["Lapis", "Papyrus", "Scalpellus"]; document.querySelector('#Lapis').onclick = setLapis document.querySelector('#Papyrus').onclick = setPapyrus document.querySelector('#Scalpellus').onclick = setScalpellus; function setLapis(){ console.log("Lapis"); } function setPapyrus(){ console.log("Papyrus"); } function setScalpellus(){ console.log("Scalpellus"); } player.currentChoice = choices[0];
 <button id="Lapis">Lapis</button> <button id="Papyrus">Papyrus</button> <button id="Scalpellus">Scalpellus</button>

 const player = { currentChoice: null } const computer = { currentChoice: null } const choices = ["Lapis", "Papyrus", "Scalpellus"]; document.querySelector('#Lapis').onclick = setLapis document.querySelector('#Papyrus').onclick = setPapyrus document.querySelector('#Scalpellus').onclick = setScalpellus; function setLapis(){ player.currentChoice = choices[0]; console.log(player.currentChoice); } function setPapyrus(){ player.currentChoice = choices[1]; console.log(player.currentChoice); } function setScalpellus(){ player.currentChoice = choices[2]; console.log(player.currentChoice); }
 <button id="Lapis">Lapis</button> <button id="Papyrus">Papyrus</button> <button id="Scalpellus">Scalpellus</button>

暂无
暂无

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

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