6

我认为它应该为 display:none 元素返回 0。但它没有,至少对于 1.10.1

<div id="a" style="display:none">
    asdf
    asdfasdf<br>
    sadf
</div>

alert($('#a').outerHeight(true))

http://jsfiddle.net/pUuAz/

4

2 回答 2

9

jQuery 为您提供元素的高度,无论它是否显示在屏幕上。

如果您希望它忽略隐藏元素,请使用以下命令:

$('#a').is(':visible') ? $('#a').outerHeight(true) : 0;
于 2013-12-04T00:49:25.707 回答
2

深入研究 $.css 到 $.style 到 $.cssHooks 到 $.cssHooks.height.get 我们看到了罪魁祸首:

function ( elem, computed, extra ) {
            if ( computed ) {
                // certain elements can have dimension info if we invisibly show them
                // however, it must have a current display style that would benefit from this
                return elem.offsetWidth === 0 && rdisplayswap.test( jQuery.css( elem, "display" ) ) ?
                    jQuery.swap( elem, cssShow, function() {
                        return getWidthOrHeight( elem, name, extra );
                    }) :
                    getWidthOrHeight( elem, name, extra );
            }
}

似乎他们交换了样式,撕掉了值,然后将其交换回显示:无。

于 2013-08-08T20:32:36.753 回答