2011-09-22 77 views
1

我想讀取IE7中div的高度。 Element.currentStyle返回「auto」。我現在如何計算這個元素的高度? jQuery如何完成這個工作?它的height()函數能夠檢索值(http://api.jquery.com/height/)當IE開發人員的酒吧顯示我的值設置爲自動?當「自動」返回時,如何獲得IE7中的css值

編輯:我不使用jQuery,所以我希望做這個工作在純JavaScript

+0

http://stackoverflow.com/questions/692523/getting-actual-height-of-an-auto-heighted-element-in -ie – JamesHalsall

回答

1

也許document.getElementById("idHere").offsetHeight有效的解決方案!

+0

謝謝!這是我正在尋找的答案。 – Mansiemans

0

我覺得這是jQuery函數計算高度:

function getWH(elem, name, extra) { 

    // Start with offset property 
    var val = name === "width" ? elem.offsetWidth : elem.offsetHeight, 
     which = name === "width" ? cssWidth : cssHeight; 

    if (val > 0) { 
     if (extra !== "border") { 
      jQuery.each(which, function() { 
       if (!extra) { 
        val -= parseFloat(jQuery.css(elem, "padding" + this)) || 0; 
       } 
       if (extra === "margin") { 
        val += parseFloat(jQuery.css(elem, extra + this)) || 0; 
       } else { 
        val -= parseFloat(jQuery.css(elem, "border" + this + "Width")) || 0; 
       } 
      }); 
     } 

     return val + "px"; 
    } 

    // Fall back to computed then uncomputed css if necessary 
    val = curCSS(elem, name, name); 
    if (val < 0 || val == null) { 
     val = elem.style[ name ] || 0; 
    } 
    // Normalize "", auto, and prepare for extra 
    val = parseFloat(val) || 0; 

    // Add padding, border, margin 
    if (extra) { 
     jQuery.each(which, function() { 
      val += parseFloat(jQuery.css(elem, "padding" + this)) || 0; 
      if (extra !== "padding") { 
       val += parseFloat(jQuery.css(elem, "border" + this + "Width")) || 0; 
      } 
      if (extra === "margin") { 
       val += parseFloat(jQuery.css(elem, extra + this)) || 0; 
      } 
     }); 
    } 

    return val + "px"; 
}