是否可以获取通过外部css文件设置的元素的背景颜色?

Bit*_*Bit 2 javascript css

我试图使用javascript获取元素的背景颜色:

网址: https ://mathemagiker.de/

Javascript: document.getElementById('angebot').style.backgroundColor

结果:没什么

但是,此元素具有背景颜色,但它是从css文件应用的.

问题:如何使用javascript获取此颜色?

DrN*_*Nio 6

试试这个:

window.getComputedStyle(document.getElementById('angebot')).backgroundColor
Run Code Online (Sandbox Code Playgroud)

结果:( "rgb(102, 185, 191)"对于您提供的URL)

之所以document.getElementById('angebot').style.backgroundColor没有返回任何东西,是因为该元素是用CSS设计的,而不是内联的.style.

The HTMLElement.style property is used to get as well as set the inline style of an element.