2016-04-01 50 views
0

当CSS高度设置为Auto时,如何获得充满文本的div对象的完整高度?当高度为自动时,jQuery OuterHeight/Height会返回错误的高度值

我试过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> 
+0

返回拨弄我的正确值。 – Goombah

+0

http://imgur.com/a/AXATT – Goombah

+0

在控制台窗口中,我得到了与你相同的228像素,但是在检测div时为252像素,它在Chrome中返回252像素的高度(尺寸工具提示)? – tonoslfx

回答

1

收益权在捣鼓我的价值观。

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

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

但你$('div')元素可是没有任何保证金,按您目前的CSS规则。

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

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

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