首页 > 解决方案 > 是否可以获得通过外部 css 文件设置的元素的背景颜色?

问题描述

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

网址: https ://mathemagiker.de/

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

结果:没有

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

问题:如何使用 javascript 获得这种颜色?

标签: javascriptcss

解决方案


尝试这个:

window.getComputedStyle(document.getElementById('angebot')).backgroundColor

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

document.getElementById('angebot').style.backgroundColor没有返回任何内容的原因是该元素是使用 CSS 而不是 inline 设置样式的.style

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


推荐阅读