5
我有一个简单的div使用一个css类,其中有一个color
和background-color
属性集。getComputedStyle返回一个CSSStyleDeclaration,但所有的属性都是空的访问
var div = document.createElement("div");
div.classList.add("my-css-class");
container.appendChild(div);
//This prints out a CSSStyleDeclaration object. This is a large object to which I see `color` and `backgroundColor`
console.log(getComputedStyle(div));
//this gives me an empty string
console.log(getComputedStyle(div).color);
//this gives me an empty string
console.log(getComputedStyle(div).getPropertyValue("color"));
为什么我无法访问CSSStyleDeclaration的属性?我知道这是从我的第一个日志。我可以看到color: "rgb(82, 194, 145)"
您可以在JSFiddle或CodePen中复制问题吗? – swider
谢谢@swider。我今天第一次和JSFiddle一起坐下,我找到了解决方案。 – Clark