最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does getPropertyValue return an empty string instead of the element's style property? - Stack Overflow

programmeradmin0浏览0评论

This seems to only happen when I use "background" in the parameters of getPropertyValue();:

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

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

Why does it return an empty string and how can I get this to return the actual background css property?

This seems to only happen when I use "background" in the parameters of getPropertyValue();:

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

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

Why does it return an empty string and how can I get this to return the actual background css property?

Share Improve this question asked Feb 20, 2012 at 16:11 David GDavid G 96.9k41 gold badges172 silver badges258 bronze badges 1
  • This behaviour persists in IE11 and FF 36.01 but Chrome 40.0.2214.115 works as expected and returns the assembled short-hand style. – Cool Blue Commented Mar 14, 2015 at 3:37
Add a ment  | 

1 Answer 1

Reset to default 8

According to this page, at least the mozilla browser returns null when requesting the value of shorthand properties. So it seems have to query the different properties of the background style separately:

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

Edit: it looks like it is a known bug

发布评论

评论列表(0)

  1. 暂无评论