getComputedStyle()在IE8

时间:2015-10-06 10:46:24

标签: jquery

我的脚本页面中有getComputedStyle()函数。但它在IE8中不起作用。

My code :- 

    var isDesktop = window.getComputedStyle(document.body,':after').getPropertyValue('content');

错误: -           对象不支持此属性或方法

1 个答案:

答案 0 :(得分:0)

对,早期IE中不存在getComputedStyle。如果您没有寻找伪元素的样式,可以使用ID currentStyle

var style = document.body.currentStyle || getComputedStyle(document.body);

...(然后使用style上的属性),但IE没有选择伪元素的能力。相反(而且这很丑陋),你必须遍历加载的样式表中的样式规则,找出哪些适用于相关元素,并从规则的样式中找出伪元素内容。

示例: (我本来会使用Stack Snippet,但它们不适用于IE8; here it is on jsbin

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Example</title>
  <style>
    body {
      color: blue;
    }
  </style>
</head>
<body>
<script>
  var style = document.body.currentStyle || getComputedStyle(document.body);
  document.body.insertAdjacentHTML(
    "beforeend",
    "<p>Color: " + style.color + "</p>"
  );
  </script>
</body>
</html>

但是,这不会给你伪元素属性。