开发者

How to get css values in IE7 when "auto" is returned

开发者 https://www.devze.com 2023-04-06 12:33 出处:网络
I\'m trying to read the height of a div in IE7. Element.currentStyle returns \"auto\". How do I now calculate the height of this element? How does jQuery accomplish this? Its height() function is able

I'm trying to read the height of a div in IE7. Element.currentStyle returns "auto". How do I now calculate the height of this element? How does jQuery accomplish this? Its height() function is able to retrieve the value(http://api.jquery.com/height/) when IE developer's bar shows me the value is set to auto?

EDIT: I'm not using jQuer开发者_如何学编程y, so I'm hoping for a solution that does this in pure javascript


Maybe document.getElementById("idHere").offsetHeight works!


I think this is the jQuery function that calculates height:

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";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消