【问题标题】:jQuery OuterHeight/Height return the wrong height value when height is auto当高度为自动时,jQuery OuterHeight/Height 返回错误的高度值
【发布时间】:2016-04-01 14:54:02
【问题描述】:

当 CSS 高度设置为 Auto 时,如何获取填充文本的 div 对象的完整高度?

我尝试了 OuterHeight,但它仍然不是 div 对象的完整高度。

JSFiddle:https://jsfiddle.net/zerolfc/jg8s5oq3/3/

jQuery:

$('div').find('*').each(function(ti,tv){
var c = parseInt( $(tv).css('font-size') ) / 2;
c = Math.floor( c );
c = ( c < 8 ? 8 : c );
$(tv).css('font-size', c + 'px');
});

console.log($('div').outerHeight(true));

CSS:

div { display: block; width: 640px; height: auto; }
p { font-size: 24px; }

HTML:

<div>
<p>This is a textbox that you can add text to suit your design and alter.This is a textbox that you can add text to suit your design and alter.</p>
<p>This is a textbox that you can add text to suit your design and alter.This is a textbox that you can add text to suit your design and alter.</p>
<p>This is a textbox that you can add text to suit your design and alter.This is a textbox that you can add text to suit your design and alter.</p>
<p>This is a textbox that you can add text to suit your design and alter.This is a textbox that you can add text to suit your design and alter.</p><p>This is a textbox that you can add text to suit your design and alter.This is a textbox that you can add text to suit your design and alter.</p><p>This is a textbox that you can add text to suit your design and alter.This is a textbox that you can add text to suit your design and alter.</p>
</div>

【问题讨论】:

标签: jquery css


【解决方案1】:

为我返回正确的值。

来自http://api.jquery.com/outerheight/

$('div').outerHeight(true) = [height of the $('div') + margin of $('div')]

但是根据您当前的 css 规则,您的 $('div') 元素没有任何边距。

这意味着, $('div').outerHeight(true) 将返回$('div') 的实际高度,没有任何边距。

在您的情况下,以下所有 3 的值都将相同。

  • $('div').outerHeight(true)
  • $('div').outerHeight()
  • $('div').css('height')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-17
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2016-09-14
    • 2012-09-29
    相关资源
    最近更新 更多