我在这里有一个jsfiddle-http://jsfiddle.net/stevea/R3z2j/2/-在样式表中将红色背景应用于主体,但是当我使用DOM查看主体中的样式节点时,

style = document.body.style;
bgclr = document.body.style.backgroundColor;


这里没有背景色。

谢谢

最佳答案

详细阐述我的评论...

element.style“代表元素的样式属性”,或元素本身的样式,来自style属性或直接操作。

要获取元素实际使用的样式(包括继承的样式),可以在getComputedStyle()中使用most browsers(可选与getPropertyValue()一起使用)或oldIE中的element.currentStyle

window.getComputedStyle(document.body).backgroundColor
window.getComputedStyle(document.body).getPropertyValue('background-color')

document.body.currentStyle.backgroundColor


或者,由于您使用的是jQuery,因此还可以使用.css(propertyName)

$(document.body).css('background-color');

关于javascript - 为什么DOM样式节点不显示样式表应用的样式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17645275/

10-09 18:05
查看更多