2014-12-02 60 views
0

我想了解jQuery如何在處理基於非像素的屬性值(如margin-top: 2em或像height: auto之類的東西)時獲得像素值。對於IE9 +,getComputedStyle()明顯可以提供這種容易,但在IE8的情況下,currentStyle不會。我試圖找到一個解決方案,以便我可以計算一個元素的總高度,包括CSS高度,填充,邊框和所有瀏覽器IE8 +的邊距。我遇到了以下答案,但我無法理解接受的答案中發生了什麼。試圖瞭解jQuery如何使用currentStyle計算IE8的計算屬性

Cross-browser (IE8-) getComputedStyle with Javascript?

我想知道是否有人能解釋什麼是這個代碼怎麼回事?

回答

0

這是來自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]; 
 
}

+0

我欣賞的答案,但我不認爲它解決了我的主要關注點,這是IE8將如何能夠訪問或性能計算非像素值,如'margin:2em;'或'height:auto;'。 – Joe 2014-12-02 21:27:40