5

如何获得由 jQuery 设置的 DIV 的高度?
$('.bar2').animate({'height':'58' + "%"},1500);

当我检查 chrome 中的元素时,我看到我的 DIV 高度设置为 58%

<div class="bar2" style="height: 58%; background-image: ......>

我试过这个:

var bar2 = $(".bar2").height(),或者var bar2 = $(".bar2").css('height'),

但我总是得到我的“最小高度”,即 70px,而不是由 jQuery 设置的高度

4

2 回答 2

3

我想你会使用:

$(".bar2").outerHeight();

哪个是计算的高度或

$(".bar2").innerHeight();

如果您不需要考虑帐户保证金和填充等等。

于 2012-06-27T16:33:51.117 回答
2

HTML:

<div style="width: 200px; height: 200px; background-color: blue;">
    <div class="bar2" style="min-height: 70px; width: 100px; background-color: red;">foo</div>
</div>

JS:

jQuery('.bar2').animate({'height':'58' + "%"}, 1500, function() {
    alert($(".bar2").css('height'))
});

现场示例 - http://jsfiddle.net/ANbrq/1/

只有在实际更改时,您才能获得不同的高度。如果您在要求它调整大小后尝试使其正确,您将获得初始高度。

于 2012-06-27T16:37:41.930 回答