0

我试图了解 jQuery 在处理基于非像素的属性值(例如margin-top: 2em,甚至类似height: auto. 对于 IE9+,getComputedStyle()显然可以轻松提供此功能,但对于 IE8,currentStyle则不能。我正在尝试找到一个解决方案,以便我可以计算元素的总高度,包括所有浏览器 IE8+ 的 CSS 高度、填充、边框和边距。我遇到了以下答案,但我无法理解接受的答案中发生了什么。

使用 Javascript 的跨浏览器(IE8-)getComputedStyle?

我想知道是否有人可以解释这段代码中发生了什么?

4

1 回答 1

0

这是来自 WebPlatform 的计算样式的 shim。

if (!window.wpo) { window.wpo = {}; }
if (!wpo.utils) { wpo.utils = {}; }

wpo.utils.getComputedStyle = function(_elem, _style)
{// wpo getComputedStyle shim.
   var computedStyle;
   if (typeof _elem.currentStyle != 'undefined')
     { computedStyle = _elem.currentStyle; }
   else
     { try{computedStyle = document.defaultView.getComputedStyle(_elem, null);}catch(e){return '';} }

  return computedStyle[_style];
}

于 2014-12-02T06:13:09.780 回答