当我尝试在 jquery 中将高度设置为 auto 并尝试再次计算它的高度时,结果将是 0。为什么会这样?
$('#test').css('height','auto')
$('#test').height(); // 0
如何计算它的高度?
编辑
这是我正在运行的 javascript 代码:
function visitFix() {
$('.visit').find('.profileDetail').each(function () {
console.log($(this).height()); //24
$(this).css('height', 'auto');
console.log($(this).height()); // 0
});
}
这就是 DOM 树的样子:
<td class="profileView">
<div class="profileContent">Purpose: </div>
<div class="profileDetail" style="height: auto;">Program Participant Volunteer Rejuvenation Participant General Visit </div>
</td>
输出是 24,然后是 0。
最佳答案
试试这个
您可以根据需要使用.height()、
.innerHeight()
或outerHeight()
。
.height() -
返回元素的高度,不包括填充、边框和边距。
.innerHeight() -
返回元素的高度,包括填充但不包括边框和边距。
.outerHeight() -
返回包含边框但不包含边距的 div 的高度。
.outerHeight(true) -
返回 div 的高度,包括边距。
关于jquery - 将 css 属性设置为 'auto' 在 jquery 中给出高度为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18010160/