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 );
            }
}

スタイルを交換し、値をリッピングしてから、それを display:none に戻すようです。

于 2013-08-08T20:32:36.753 に答える