如何获取分配给html表的CSS属性? [英] How to get CSS attributes assigned to a html table?
本文介绍了如何获取分配给html表的CSS属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个HTML表,其中每一行都有一些样式类(通过服务器端代码动态分配)。
I have an HTML table where each row has some style classes assigned (dynamically through server side code).
HTML看起来像:
<tr id= "2977" class ="x y z a b c" >
我使用此访问行的背景颜色:
I'm using this to access the background color of a row:
document.getElementById("2977").style.backgroundColor
但我不能读取任何样式属性;上面的行返回 null
。如果我检查Chrome中的行元素,它显示行的十六进制颜色。
But I am not able to read any of the style attributes; the above line returns null
. If I inspect the row element in Chrome, it shows me hex-color of the row.
如何读取元素的当前背景颜色?
How can I read the current background color of the element?
推荐答案
p>此 http://jsfiddle.net/UvYxc/ 工作
HTML
<table>
<tr style="background:red;" id= "2977" class ="x y z a b c" >
<td>RED</td>
</tr>
</table>
JS
document.getElementById("2977").style.backgroundColor="blue"
alert(document.getElementById("2977").style.backgroundColor);
或
document.getElementById("2977").className += " blue";
document.getElementById("2977").className += " red";
function getStyle(el, cssprop){
//IE
if (el.currentStyle)
return el.currentStyle[cssprop]
//Firefox
else if (document.defaultView && document.defaultView.getComputedStyle)
return document.defaultView.getComputedStyle(el, "")[cssprop]
else //try and get inline style
return el.style[cssprop]
}
var el = document.getElementById("2977");
alert(getStyle(el, "backgroundColor"));
alert(getStyle(el, "color"));
小提琴此处
这篇关于如何获取分配给html表的CSS属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文