Tôi có một div đơn giản sử dụng một lớp css mà lần lượt có một thuộc tính color
và background-color
.getComputedStyle trả về một CSSStyleDeclaration nhưng tất cả các thuộc tính đều trống trên truy cập
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"));
Tại sao tôi không thể truy cập các thuộc tính của CSSStyleDeclaration? Tôi biết nó có từ nhật ký đầu tiên của tôi. Tôi có thể thấy color: "rgb(82, 194, 145)"
Bạn có thể sao chép sự cố trong JSFiddle hoặc CodePen không? – swider
Cảm ơn bạn @swider. Hôm nay tôi đã ngồi xuống với JSFiddle, và tôi đã tìm ra giải pháp. – Clark