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

Cross-browser (IE8-) getComputedStyle with Javascript?

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

最佳答案

这是WebPlatform中计算样式的填充。



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];
}

09-27 13:06