style.backgroundColor="red"; //add
style.backgroundColor="";    //remove

解决方案 »

  1.   

    ...
    <style type="text/css">
    <!--
    .red{
    background-color:red;
    }
    -->
    </style>
    ...
    <div id="divTest" class="red">asdfasdfsdaf</div>
    <input type="button" id="btnClassName" onclick="var a=document.getElementById('divTest');if(a.className=='red'){a.className='';}else{a.className='red';}" value="Change ClassName"/>
    ...
      

  2.   

    meizz(梅花雪)就是说赋空值就是移除,就会自动使用red 这个class了吧?
      

  3.   

    刚试过meizz(梅花雪)的方法, 不行, 
    style.backgroundColor="";    //移除后背景色会变成白色, 而不是使用原来 class 指定的颜色的
      

  4.   

    可以通过改变样式表的color来实现~
      

  5.   

    DeluxWorld(曾经的你)具体怎样写法呢?还有能否取得className这个CSS里指定景背景色值,如果可以就可再用style 指定为这个值也可以
      

  6.   

    td.className = 'className'可以不?
      

  7.   

    做两套样式表,用className切换
      

  8.   

    xxuu503(我爱郭芙蓉!) 
    一开始就试过了, 而且还试过把className删除掉后再加上也不行
      

  9.   

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <style type="text/css">
    <!--
    body {TEXT-ALIGN: center;}
    #aa {
    background-color: #33CC66;
    }
    #center {
    MARGIN-RIGHT: auto;
    MARGIN-LEFT: auto;
    width: 700px;
    text-align: left;

    -->
    </style>
    </head>
    <script language="JavaScript" type="text/javascript">
    function aa(){
    document.getElementById('ff').style.background = "#33CC66";
    }
    </script><body>
    <div id="center">dfffff</div>
    <table width="100%" border="0" cellspacing="0" cellpadding="5">
      <tr>
        <td id="ff" style="background-color: #006666;">&nbsp;</td>
      </tr>
    </table><label>
    <input type="button" onclick="aa();" name="Submit" value="按钮" />
    </label>
    </body>
    </html>
      

  10.   

    ancomsj(fsdjfk)你是重设回原来的颜色值,问题是程序怎样取得css里设定的颜色值呀
      

  11.   

    我觉得没那么复杂吧,你在className里不要指定表格的背景色,而在style里指定,会不会更好些呢?
      

  12.   

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <style>
    .className {
    border: 1px solid #0000FF;
    }
    .className td{
    border: 0px none none;
    }
    </style>
    <body>
    <script language="JavaScript" type="text/javascript">
    function changeBgColor(){
    var tbl = document.getElementById("tbl_demo");
    var defaultBackgroundColor = "#FF0000";
    if (tbl.style.backgroundColor.toUpperCase() == defaultBackgroundColor.toUpperCase()){
    tbl.style.backgroundColor = "#00FF00";
    } else {
    tbl.style.backgroundColor = defaultBackgroundColor;
    }
    }
    </script><table id="tbl_demo" width="400" border="1" cellspacing="1" cellpadding="3" class="className" style="background-color:#FF0000;">
      <tr>
        <td scope="row">&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td scope="row">&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <form id="form1" name="form1" method="post" action="">
      <input type="button" name="Submit" value="变色" id="Submit" onclick="changeBgColor();" />
    </form>
    </body>
    </html>
      

  13.   

    把我上面的改改,不就完了么  ...
    <style type="text/css">
    <!--
    .red{
    background-color:red;
    }
    .green{
    background-color:green;
    }
    -->
    </style>
    ...
    <div id="divTest" class="red">asdfasdfsdaf</div>
    <input type="button" id="btnClassName" onclick="var a=document.getElementById('divTest');if(a.className=='red'){a.className='green';}else{a.className='red';}" value="Change ClassName"/>
    ...
      

  14.   

    xeonwell(xeonwell) 如果是你这种方式就好办了, 但现在程序.red所指定的颜色是基色, 之后可以由用户自己来改颜色, 什么颜色是不定的, 给个选色器用户自己先, 所以用你的方法不行呀,如果是这样早就解决了KimSoft(革命的小酒天天醉)的方法还可以考虑下, 我开始也想过用公共变量保存这个色值,但这样深感就是不方便的,如果真的没办法也只有这样了
      

  15.   

    不方便之处在于我的页面有很多这样的表格, 每个表格用的css都不同, 所以颜色值也不同, 如果这样就要为每一个定义 一个全局变量,而且还要指定它是属于那个表格, 这样也是很头痛的事
      

  16.   

    解决了, 把CSS里所指定的颜色值读出来赋给当前的style就行了