2012-02-20 93 views
3

這似乎只發生在我的getPropertyValue();參數使用「背景」:爲什麼getPropertyValue返回空字符串而不是元素的樣式屬性?

var d = document.getElementById('myDiv'); 

window.getComputedStyle(d).getPropertyValue('background'); // "" 

爲什麼它返回一個空字符串,我怎麼能得到這個返回的實際背景的CSS屬性?

+0

這種行爲在IE11堅持和FF 36.01而Chrome 40.0.2214.115作品如預期的那樣,並返回組裝好的短手型。 – 2015-03-14 03:37:26

回答

7

根據this page,當請求值shorthand properties時,至少mozilla瀏覽器返回null。因此,似乎必須單獨查詢的背景樣式的不同屬性:

window.getComputedStyle(d).getPropertyValue('background-color'); 
window.getComputedStyle(d).getPropertyValue('background-image'); 
// etc. 

編輯:看起來它是一個known bug

相關問題