请问当按一个按钮时,颜色改变,其它的按钮不变?
例如有4个按钮,都是黑色的,按下任意一个时,那个按钮就变白,又按其它,那个白的又变回黑色.
就是始终只有一个按钮是白色的,请问怎样实现?
------解决方案-------------------- <script language= "javascript ">
function mm(a){
var b=document.getElementsByName( "btn ")
for(var i=0;i <b.length;i++){
if(a==b[i]){
a.style.background= " "
}else{
b[i].style.background= "black "
}
}
}
</script>
<input type= "button " value= "1 " name = "btn " onclick= "mm(this) " style= "background:black ">
<input type= "button " value= "2 " name = "btn " onclick= "mm(this) " style= "background:black ">
<input type= "button " value= "3 " name = "btn " onclick= "mm(this) " style= "background:black ">
<input type= "button " value= "4 " name = "btn " onclick= "mm(this) " style= "background:black ">
------解决方案-------------------- <style>
.black{border:#000000 solid 2px;background-color:#000000;width:100px;height:60px;margin-right:10px;}
.white{border:#000000 solid 2px;background-color:#FFFFFF;width:100px;height:60px;margin-right:10px;}
</style>
<script>
var old = null;
function press(obj){
if(old != null) old.className= "black ";
obj.className= "white ";
old = obj;
}
</script>
<span class= "black " onclick= "press(this) "> </span>
<span class= "black " onclick= "press(this) "> </span>
<span class= "black " onclick= "press(this) "> </span>
<span class= "black " onclick= "press(this) "> </span>