【发布时间】: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>
【问题讨论】:
-
在小提琴中为我返回正确的值。
-
在控制台窗口中,我得到与您相同的 228px 但在检查 div 时为 252px,它在 Chrome 中返回 252px 的高度(尺寸工具提示)?