我有一个span从一堆样式里动态获得了一个样式,然后在后面的程序里临时让它用了另一个样式,用完后还要让它还原到原来的样式,但我不知道它原来是什么样式,这个怎么弄啊?能不能像变量赋值一样:c=a,a=b,a=c.弄个中间过渡的变量存放一下啊?

解决方案 »

  1.   

    span.setAttribute("oldStyle","xxx")
    span.style.xxx = span.getAttribute("oldStyle")采用伪属性保存
      

  2.   


    可以,表格的光棒效果也是这样实现的<table width="100%">
    <tr style="background-color:#666" onmouseover="oldcolor = this.style.backgroundColor;this.style.backgroundColor='#999'" onmouseout="this.style.backgroundColor=oldcolor"><td>test</td></tr>
    <tr style="background-color:#666" onmouseover="oldcolor = this.style.backgroundColor;this.style.backgroundColor='#999'" onmouseout="this.style.backgroundColor=oldcolor"><td>test</td></tr>
    <tr style="background-color:#666" onmouseover="oldcolor = this.style.backgroundColor;this.style.backgroundColor='#999'" onmouseout="this.style.backgroundColor=oldcolor"><td>test</td></tr>
    </table>
      

  3.   

    哦,原来这就叫光棒效果啊……
    贴个直接用classname的代码 <select onchange="setStyle(this.value)">
     <option>请选择</option>
     <option value="cls1">样式红</option>
     <option value="cls2">样式蓝</option>
     <option value="cls3">样式黄</option>
     </select>
     <BR>
    <span id="sp" style="width:100px; height:100px;" class="cls0">span</span>
    <BR>
    <INPUT id="btn" TYPE="button" VALUE="还原上一样式" ONCLICK="turnback()" disabled>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    var oldStyle;
    function setStyle(para){
    oldStyle = sp.className;
    sp.className = para;
    btn.disabled = false;
    }
    function turnback(){
    sp.className = oldStyle;
    btn.disabled = true;
    }
    //-->
    </SCRIPT>
      

  4.   

    你这个xxx是不是只是单个属性啊?如果我的样式群都像.style1{color:red;font-size:12px;......}等多个属性怎么办?
      

  5.   

    可不可以xxx=document.getElementID("span").classname
    然后document.getElementID("span").classname=xxx