【发布时间】:2014-09-22 05:06:56
【问题描述】:
我正在尝试通过将框高度调整为溢出内容高度来修复溢出的浮动内容,但它似乎并没有起到作用。
if ( $('.content-right').outerHeight() > $('.content-right').parent().parent().height() ) {
$('.content-right').parent().parent().height($('.content-right').outerHeight(true));
}
console.log('Box Height: ' + $('.content-right').parent().parent().height());
console.log('Content Height: ' + $('.content-right').height() );
这将输出
Box Height: 599
Content Height: 594
这是不正确的,因为在下面的示例中 div 显然要大得多。有任何想法吗?
图片形式的问题区域:http://prntscr.com/4p1obb
【问题讨论】:
-
如果你仔细阅读文档:api.jquery.com/height 它写得很清楚:
This method does not accept any arguments. -
不要使用
.parent().parent(),而是看看api.jquery.com/closest方法。 -
另外,缓存元素你打算多次使用并且过度使用
var $contRight = $('.content-right'); -
@RokoC.Buljan 这是此处编辑的错误。是
outerHeight(true)没有一个 cmets 是针对这个问题和挑剔的。 -
我不知道。我只是建议查看 you 提供的代码。你确定你得到了正确的元素吗?我看到你使用
class选择器元素。可能会返回错误的吗?
标签: jquery height outerheight