2012-02-03 39 views
1

我已經佈置了一些CSS樣式選擇「HTML」這樣的分配樣式屬性:document.documentElement中不保持與「HTML」選擇

<style type="text/css"> 
html { background-color:blue; } 
</style> 

通過JavaScript,我想訪問風格這樣的屬性(示例):

alert(document.documentElement.style.backgroundColor); 

在Chrome中,該屬性爲空,在FF中它是一個空字符串。看來我可以通過CSS選擇器訪問html元素,但不能通過document.documentElement通過javascript ...嗯,任何關於如何訪問由CSS選擇器設置的樣式(通過JavaScript)的建議嗎?

由於提前,賴因

回答

2

此:

window 
    .getComputedStyle(document.documentElement) 
    .getPropertyValue('background-color') 

現場演示:http://jsfiddle.net/5jtqC/

style屬性只讀取內嵌樣式,而不是由一個樣式表中定義的樣式。

0

element.style僅通過style屬性處理內聯樣式。

要獲得元素的當前樣式,使用方法:

(element.currentStyle || getComputedStyle(element)).backgroundColor;