jquery - 使用jquery高度动态获取两个嵌套框的高度?

标签 jquery css height

我想根据内容的长度动态获取框(在我的示例中为黄色背景)的高度。所以如果它有一个长文本,它的高度会增加。这个黄色盒子有一个父容器(在我的例子中是蓝色背景)。

我不明白的是为什么它们具有相同的高度 - 即 100

蓝色框应该有更大的数字,因为它有padding top 20pxpadding bottom 20px。因此,如果黄色框为 100,则蓝色框应为 140

j查询,

// Set the object.
var object = $(".box");
//var object_height = parseInt(object.css('min-height'),10) || parseInt(object.css('height'),10);
var object_height = object.height();

var scrollable = $(".scrollable");
//var scrollable_height = parseInt(scrollable.css('min-height'),10) || parseInt(scrollable.css('height'),10);
var scrollable_height = scrollable.height();

alert(object_height);
alert(scrollable_height);

html,

<div class="box">

    <div class="scrollable">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>

</div>

链接来自 jsfiddle .

任何想法如何让这个正确?

最佳答案

你应该使用 outerHeight() 方法,它计算高度 + 边距/填充

关于jquery - 使用jquery高度动态获取两个嵌套框的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12548766/

相关文章:

html - 将侧边栏拉伸(stretch)到 100% 高度

jquery - 将第一个 child 的高度分配给 parent

javascript - 等待 JQuery div 加载

php - 想知道图像被拉到哪里

html - angular2 html并不总是加载

jquery - 如何设置每个列表项相同的高度?

jQuery 防止将一个元素放入另一个列表

jquery - 绑定(bind) Flot 饼图时,即使数据存在,它也不可见

javascript - 使用箭头键作为事件监听器

css - 是否有使用 scss 全面覆盖 ion-view ion-title 字体颜色的方法?