当我尝试将高度设置为div以在jquery中自动设置并尝试再次计算其高度时,结果为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。

最佳答案

尝试这个

http://jsfiddle.net/zS7kd/

您可以根据需要使用.height(), .innerHeight()outerHeight()

http://api.jquery.com/height/



.height() -返回元素的高度,不包括填充,边框和边距。

.innerHeight() -返回元素的高度,包括填充,但不包括边框和边距。

.outerHeight() -返回div的高度,包括边框,但不包括边距。

.outerHeight(true) -返回div的高度,包括边距。

关于jquery - 将CSS属性设置为'auto'在jQuery中将高度设置为0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18010160/

10-12 12:50
查看更多