jquery获取高度错误(可以获取到宽度,但获取不到高度),及解决办法

时间:2023-03-09 07:27:28
jquery获取高度错误(可以获取到宽度,但获取不到高度),及解决办法
<div   class="foo">
<div style="display: none;">
3333333
</div>
</div>

jquery获取一个元素的高度,但元素的子元素设置display:none;

<script type="text/javascript">

alert($('.foo').height());
alert($('.foo').width());
</script>

或设置了$('.foo').hide();

$('.foo').height()将获取不到高度。

解决办法是在获取高度前显示,获取后隐藏

$('.foo').show();
$height=$('.foo').height();
$('.foo').hide();