2011-09-22 79 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"; 
}