gpt4 book ai didi

当高度为自动时,jQuery OuterHeight/Height 返回错误的高度值

转载 作者:太空宇宙 更新时间:2023-11-04 10:33:23 27 4
gpt4 key购买 nike

当 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>

最佳答案

为我返回正确的值。

来自 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')

关于当高度为自动时,jQuery OuterHeight/Height 返回错误的高度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36359490/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com