1

This is my problem:

var mycss = window.getComputedStyle(myelement);

returns a CSSStyleDeclaration object:

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

console.log(mycss);

Then, I want to get the background color, but

mycss.getPropertyValue("backgroundColor");

returns an empty string ""!

Why??

4

2 回答 2

2

代替

mycss.getPropertyValue("backgroundColor");

利用

mycss.getPropertyValue('background-color')

这对我有用。

于 2018-01-17T17:41:42.093 回答
1

在您的 CSSStyleDeclaration 中,您需要将 'backgroundColor' 更改为 'background-color' 然后调用

mycss.getPropertyValue('background-color')

一个例子:HTML:

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

然后调用 getPropertyValue:

var mycss = window.getComputedStyle(document.getElementById("body"));
myelement.innerHTML = mycss.getPropertyValue("background-color");
于 2018-01-17T18:01:29.357 回答