<table>
<tr>
<td id="td1" style="background-color:#00FF00" onclick="test()">adf</td>
</tr>
</table>
<SCRIPT LANGUAGE=javascript>
<!--
function test(){
alert(td1.style.backgroundColor)
}
//-->
</SCRIPT>

解决方案 »

  1.   

    no probelm<table border=1><tr>
    <td id="td1" style="background-color:#00FF00" onclick=alert(td1.style.backgroundColor)>adf</td>
    </tr></table>
      

  2.   

    咦,这样是可以哦,但是我在 css 文件中写这个就会取不到了为什么?
      

  3.   

    我一开始都没注意到这个问题,我以为css文件中写 style
    和直接写style应该是一样的
      

  4.   

    你是不是写在class中了?那正常呀,那时backgroundColor本来就是空嘛。
      

  5.   

    <Style>
    #td1{background-color:#00FF00}
    </style>
    <table border=1><tr>
    <td id="td1" onclick=alert(td1.currentStyle.backgroundColor)>adf</td>
    </tr></table>
      

  6.   

    只是显示出来的效果一样,但用JAVASCRIPT取值得时候,就不一样了,JAVASCRIPT不认识CSS文件。
      

  7.   

    使用class好了,不要直接引用backgroundcolor
    onMouseOve="this.style.class='ClassOver'"
      

  8.   

    用runtimeStyle引用运行时刻的样式属性..(即使在随后的脚本中更改多样式也可以取到)
    用style能引用的仅仅是内联样式中所定义的静态样式
      

  9.   

    style + runtimeStyle => currentStyle
      

  10.   

    即时style(runtimeStyle)和属性style共同作用出当前style(currentStyle)
      

  11.   

    runtimestyle(运行时样式)用来即时改变元素的样式,这里所说的的“即时”,指的是这样的改变并不改变元素的style,runtimeStyle只是起到改变的作用,它自己本身在没有使用之前没有值。<div id=a1 style="color:red">runtimeStyle和currentStyle的例子!点击runtimeStyle按钮</div>
    <script>document.write("初始的style是:"+a1.style.color+"<br>初始的runtimeStyle是:"+a1.runtimeStyle.color+"<br>初始的currentStyle是:"+a1.currentStyle.color)</script><br>
    <input type=button value=runtimeStyle onclick="a1.runtimeStyle.color='blue';show()">
    <input type=button value=恢复本来的颜色 onclick="a1.runtimeStyle.color=a1.style.color">
    <br>
    <script>
    function show(){
    alert("当前的style是:"+a1.style.color+"\r当前的runtimeStyle是:"+a1.runtimeStyle.color+"\r当前的currentStyle是:"+a1.currentStyle.color+"\r\r看到了吧,style依然没有变化,可是当前显示的颜色是蓝色,currentStyle显示的就是当前的颜色;使用a1.runtimeStyle.color=a1.style.color就可以恢复原来的颜色");
    }</script><hr>
    <style>
    p{color:red}
    </style>
    在不是行内定义的样式情况下,style可能显示的值是空,这是就可以使用currentStyle
    <p onclick='alert("this.style.color = "+this.style.color+"\rthis.currentStyle.color = "+this.currentStyle.color)'>currentStyle的例子!点击这里</p>
      

  12.   

    在javascript中对元素的属性名style.xxx和css中的属性名不一定一要的,如backgroundcolor和background-color