获取伪元素的背景样式

时间:2014-01-31 11:16:44

标签: javascript jquery internet-explorer cross-browser pseudo-element

Internet Explorer 8 不允许打印背景图像 不幸的是,我被要求做这项工作。

有重要的&#34;某些伪元素中的图像必须是可打印的 我认为可以将这些背景图像转换为<img/>元素 但是,似乎无法在Internet Explorer 8中读取伪元素的样式,因为它不支持getComputedStyle

有什么好主意吗?

相关:
https://developer.mozilla.org/en/docs/Web/API/window.getComputedStyle
http://caniuse.com/getcomputedstyle/embed
SO "Getting pseudo-element style values"

1 个答案:

答案 0 :(得分:3)

这应该有效:

var img = window.getComputedStyle(
    document.querySelector('.element'), ':before'
).getPropertyValue('background');

修改:我的working fiddle不使用querySelector

var img = window.getComputedStyle(
    $(".element")[0], ':before'
).getPropertyValue('background');

基本上,您只想在getComputedStyle;

中获取元素作为第一个参数

但仍然没有修复IE 8的要求,因为那里不支持getComputedStyle,但是...... this question可能会提供答案。