javascript - 为什么 getPropertyValue 返回一个空字符串而不是元素的样式属性?

标签 javascript css

这似乎只有当我在 getPropertyValue(); 的参数中使用“背景”时才会发生:

var d = document.getElementById('myDiv');

window.getComputedStyle(d).getPropertyValue('background'); // ""

为什么它返回一个空字符串,我怎样才能让它返回实际的背景 css 属性?

最佳答案

根据 this page ,至少 mozilla 浏览器在请求 shorthand properties 的值时返回 null .所以好像要分别查询背景样式的不同属性:

window.getComputedStyle(d).getPropertyValue('background-color');
window.getComputedStyle(d).getPropertyValue('background-image');
// etc.

编辑:它看起来像是一个 known bug

关于javascript - 为什么 getPropertyValue 返回一个空字符串而不是元素的样式属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9364367/

相关文章:

html - 为 fontawesome 图标添加白色背景

javascript - td 元素上的 OnClick 函数返回第一列值和标题值

javascript - react 问题和 "this"即使在绑定(bind)后

HTML 中的 Javascript : How to insert a user input into the middle of a string automatically?

Javascript:getElementsByClassName 未给出所有元素

html - CSS - 当第一个 div 溢出时如何强制第二个 div 到另一行

javascript - 使用 Jquery 将跨度添加到数字

html - 向此响应式下拉菜单添加第二层

jquery - 是否有 jQuery 替代位置 : sticky?

html - 如何用边界边上的链接创建一个圆