JQuery outerHeight()在hidden(display:none)元素上返回非零值?

时间:2013-08-08 20:15:53

标签: javascript jquery outerheight

我认为应该为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/

2 个答案:

答案 0 :(得分:8)

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

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

$('#a').is(':visible') ? $('#a').outerHeight(true) : 0;

答案 1 :(得分: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 );
            }
}

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

相关问题