jquery - 将 css 属性设置为 'auto' 在 jquery 中给出高度为 0

标签 jquery css

当我尝试在 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。

最佳答案

试试这个

http://jsfiddle.net/zS7kd/

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

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

enter image description here

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

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

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

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

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

相关文章:

php - jquery post 显示加载消息直到成功

css - IE 的网络测试。 IETester 的准确性如何?

javascript - 如何控制分层边缘捆绑图中线条的粗细?

ruby-on-rails - capistrano 部署后未加载 css 文件

jquery - 当我点击一个元素时检测之前点击了哪个元素

javascript - jQuery UI Vertical Tabs 使页面滚动 "stick"

jquery - 禁用“提交”按钮,直到填写输入字段

html - 将 div 通过它们的中间对齐到屏幕中间

jQuery 和 CSS 冒号选择器

javascript - jQuery 背景图片淡入淡出功能导致 Chrome 和 Opera CPU 使用率高