2017-01-05 233 views
-1

我認爲自CSS 3可以使用Background作爲Background-color屬性。它說顯示的顏色是基於Desktop-Color的,所以它可以從客戶端到客戶端變化。 alert(document.body.style.backgroundColor);僅返回background,而不是顏色值。 那麼如何獲取特定元素中使用顏色的RGB-/Hex-Value?如何在JavaScript中獲取「背景顏色:背景」顏色?

+1

您能得到什麼,如果你使用的getComputedStyle'()'? – Barmar

+0

document.body.style.backgroundColor對我來說很好用 –

+0

'backgroundColor'返回背景顏色,而不是整個背景。並且'background-color'已經在CSS 2.1 – Oriol

回答

1

使用getComputedStyle()

// $0 is your htmlElement 
window.getComputedStyle($0, null).getPropertyValue('background-color') 
+1

現代瀏覽器不需要'null' – Oriol

+0

@Oriol很高興知道,無論如何,我爲了兼容性,我將把它留在那裏 –