微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单击按钮时如何更改const值?

如何解决单击按钮时如何更改const值?

有人可以帮助我用可以帮助我根据单击哪个按钮来更改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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。