我有两个button,两个的颜色本来是一样的,想点击一个button的时候颜色改变,再点击另一个的时候颜色能够切换,请问应该如何实现?
我只知道如何单个的改变button的背景颜色,用onclick="this.style.bggroundColor='#ffffff'";但是没有想到如何来切换两个的颜色变化。谢谢!

解决方案 »

  1.   

    看看这个例子
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" > 
    <head> 
        <title>改变颜色</title> 
    </head> 
    <body> 
    <p>&nbsp;</p>
    <input name="btn01" type="button" id="btn01" value="button01" style="background-color:#FF0000; " onclick="javascript:chan('btn01');">&nbsp;
    <input name="btn02" type="button" id="btn02" value="button02"  onclick="javascript:chan('btn02');">
    <script language="javascript" type="text/javascript">
    function chan(id)
    {
    document.getElementById("btn01").style.backgroundColor="";
    document.getElementById("btn02").style.backgroundColor="";
    var o=document.getElementById(id);
    o.style.backgroundColor="#FF0000";  //根据你的 需要修改颜色即可
    }
    </script>
    </body> 
    </html>