Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得;背景色:背景-JavaScript中的颜色?_Javascript_Css - Fatal编程技术网

如何获得;背景色:背景-JavaScript中的颜色?

如何获得;背景色:背景-JavaScript中的颜色?,javascript,css,Javascript,Css,我认为既然css3你可以使用Background作为Background color属性。它说显示的颜色基于桌面颜色,所以它可以在不同的客户端之间变化。 alert(document.body.style.backgroundColor)只返回背景,而不是颜色值。 那么,如何获得特定元素中所用颜色的RGB-/Hex值呢?使用 如果使用getComputedStyle(),您会得到什么?document.body.style.backgroundColor对我来说很好backgroundColor

我认为既然
css3
你可以使用
Background
作为
Background color
属性。它说显示的颜色基于桌面颜色,所以它可以在不同的客户端之间变化。
alert(document.body.style.backgroundColor)
只返回
背景
,而不是颜色值。 那么,如何获得特定元素中所用颜色的RGB-/Hex值呢?

使用


如果使用
getComputedStyle()
,您会得到什么?document.body.style.backgroundColor对我来说很好
backgroundColor
返回背景颜色,而不是完整背景。而且
background color
已经在CSS 2.1中定义了感谢大家,
getComputedStyle
为我工作:)我想提供一些建议:如果你能找到避免它的方法,不要使用JS设置样式-而是添加/删除要由CSS选择和设置样式的类。帮助简化代码维护现代浏览器不需要
null
@Oriol很好知道,无论如何,为了兼容性,我将把它留在那里
// $0 is your htmlElement
window.getComputedStyle($0, null).getPropertyValue('background-color')