Firefox 5在运行getComputedStyle时提供所有CSS

时间:2011-08-08 10:06:53

标签: javascript firefox

运行时使用firefox 5

window.getComputedStyle(document.getElementsByTagName("img")[0], null);

我获得了完整的css文件,而不是应用于“img”标签的样式。

我在https://developer.mozilla.org/en/DOM/window.getComputedStyle

上运行了这个

任何人都知道一种解决方法吗?

3 个答案:

答案 0 :(得分:1)

它应该为您提供类型为ComputedCSSStyleDeclaration的对象,其中包含已设置的所有样式。这包括所有可能的样式,而不仅仅是那些以某种方式被你操纵的样式。

为了获得特定规则,请使用例如:

window.getComputedStyle(document.getElementsByTagName("img")[0], null)['borderLeftColor'];

这给出了左边框颜色,而没有区分指定/计算的值。

要获取可用条目的列表,请将对象打印到Firebug的控制台:

console.dir(window.getComputedStyle(document.getElementsByTagName("img")[0], null));

答案 1 :(得分:1)

我知道这是一个较旧的帖子,但是对于任何登陆这里的人来说。

基本思路:您需要在getPropertyValue()返回的对象上调用window.getComputedStyle()方法。

看到这个小提琴:http://jsfiddle.net/zupa/jyyt9/

MDN表示您无需致电document.defaultView.getComputedStyle()window.getComputedStyle

注意,window.getComputedStyle()返回已使用的值而非计算值。 (见上一个链接。)

兼容性表:MDNquirksmode

答案 2 :(得分:0)

使用此:

document.defaultView.getComputedStyle(document.getElementsByTagName("img")[0], "");