2012-06-27 140 views
5

如何獲得DIV的高度,這是由jQuery設置的?
$('.bar2').animate({'height':'58' + "%"},1500);jQuery元素的高度

當我在Chrome檢查元素我看到我的DIV的高度設定爲58%

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

我已經試過這樣:

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

但我總是得到我的「最小高度」是70px,而不是由jQuery設置的高度

回答

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/

你只能得到一個不同的高度時,它的實際變化。如果您在要求重新調整大小後嘗試正確地獲取它,則會獲得初始高度。

+0

這對我有用,謝謝 –

3

我想你會用:

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

這是計算的高度或

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

如果你不需要考慮margin和padding和什麼,而不是。

+1

'$ .outerHeight()'將在返回的值中包含元素的邊框和填充。不幸的是,你的回答[不起作用](http://jsfiddle.net/jamwaffles/BTcfe/)。 – Bojangles

+0

在您的示例中,高度將爲50像素,因爲它沒有任何折扣可達58%,因此最小高度會起作用。 –

+1

我把我的評論帶回去; [這一個](http://jsfiddle.net/jamwaffles/BTcfe/1/)的作品,在''上設置了一個高度。 – Bojangles