2013-03-15 146 views
1

我需要使用Web存儲API來將元素的樣式更改存儲在一個頁面上。 我其實並不知道如何做到這一點,但我想我會首先獲取已更改並將其存儲在數組中的CSS屬性。我試圖按照什麼去here和剪裁我的問題。從元素獲取CSS值

這是我爲了獲取值但林不知道這是否是正確的已經試過:

function newItem(){ 
    var bgImg = document.getElementsByTagName('body').bgImg[0].style.getPropertyValue('background'); 
    var wideScreen = getElementById('sidebar').style.getPropertyValue('display'); 
    var playerColor = getElementById('video_container').style.getPropertyValue('background-color');  
    } 

我不知道如果我上面寫的代碼抓住我需要的信息。

+0

你爲什麼不確定?你有沒有測試過它? – Daedalus 2013-03-15 04:26:42

回答

1

您可以使用getComputedStyle().

getComputedStyle()給出了一個元素的所有CSS屬性的最終使用的值。

var element = document.getElementById('sidebar'), 
    style = window.getComputedStyle(element), 
    display = style.getPropertyValue('display'); 


var element = document.getElementById('video_container'), 
    style = window.getComputedStyle(element), 
    bg = style.getPropertyValue('background-color');