我有 3 个绿色按钮,我想在单击按钮时将颜色更改为红色,例如:单击按钮 1,按钮从绿色变为红色,然后单击按钮 2,按钮 1 变回绿色现在按钮 2 是红色的。
现在我有:
var button1 = document.getElementById("button1");
var count= 0;
button1.onclick = onbutton1clicked;
function onbutton1clicked(){
count++;
button1.innerHTML=count;
if(onbutton1clicked){
button1.style.backgroundColor = "red";
}else{
button1.style.backgroundColor = "green";
}
}
var button2 = document.getElementById("button2");
var count= 0;
button2.onclick = onbutton2clicked;
function onbutton2clicked(){
count++;
button2.innerHTML=count;
if(onbutton2clicked){
button2.style.backgroundColor = "red";
}else{
button2.style.backgroundColor = "green";
}
}
var button3 = document.getElementById("button3");
var count= 0;
button3.onclick = onbutton3clicked;
function onbutton3clicked(){
count++;
button3.innerHTML=count;
if(onbutton3clicked){
button3.style.backgroundColor = "red";
}else{
button3.style.backgroundColor = "green";
}
}
但是当点击其他按钮时,第一个按下的按钮保持红色,我该如何解决这个问题?
繁星coding
哈士奇WWW
跃然一笑
九州编程
相关分类