getPropertyValue(“backgroundColor”) returns an empty string

瘦欲@ 提交于 2021-01-28 10:21:02

问题


This is my problem:

var mycss = window.getComputedStyle(myelement);

returns a CSSStyleDeclaration object:

CSSStyleDeclaration {0: "animation-delay",..., backgroundColor: "rgb(0, 0, 0)",...}

Then, I want to get the background color, but

mycss.getPropertyValue("backgroundColor");

returns an empty string ""!

Why??


回答1:


Within your CSSStyleDeclaration, you need to change 'backgroundColor' to 'background-color' and then call

mycss.getPropertyValue('background-color')

An example: HTML:

<head><style>
body {
    background-color: lightblue;
}
</style>
</head>
<body id="body">
  hello world
</body>

and then calling the getPropertyValue:

var mycss = window.getComputedStyle(document.getElementById("body"));
myelement.innerHTML = mycss.getPropertyValue("background-color");



回答2:


Instead of

mycss.getPropertyValue("backgroundColor");

use

mycss.getPropertyValue('background-color')

That worked for me.



来源:https://stackoverflow.com/questions/48306795/getpropertyvaluebackgroundcolor-returns-an-empty-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!