jQuery元素的高度
问题描述:
如何获得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设置的高度
答
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/
你只能得到一个不同的高度时,它的实际变化。如果您在要求重新调整大小后尝试正确地获取它,则会获得初始高度。
答
我想你会用:
$(".bar2").outerHeight();
这是计算的高度或
$(".bar2").innerHeight();
如果你不需要考虑margin和padding和什么,而不是。
这对我有用,谢谢 –