javascript - jQuery:将 <section> 的高度设置为其所有子项的高度?

标签 javascript jquery html css

我有这个……

<section class="cat">
   <article class="layer"></article>
   <article class="layer"></article>
   <article class="layer"></article>
   <article class="layer"></article>
</section>

<section class="cat">
   <article class="layer"></article>
   <article class="layer"></article>
   <article class="layer"></article>
</section>

我想将每个 section.cat 的高度设置为其所有子项的高度。 我想用 jquery 做到这一点,所以我在所有浏览器中都兼容。似乎没有办法使用纯 css,因为我的 article.layer 设置为 display:table ...

article.layer {
    position: relative;
    display: table;
    height: 100%;
    width: 100%;
}

因此所有文章都是当前视口(viewport)的 100% 高度,因为我的 bodyhtml 也设置为 100% 高度。

现在 section.cat 被设置为 display:inline ,它在大多数浏览器中有效,但在 Safari 5.0 中无效。

section.cat {
    display:inline;
}

因此我想用 jquery 来做到这一点并获取 section.cat 中每个 child 的高度并将其高度设置为此值。否则 section.cat 的高度为 0,如上所述 Safari 5.0 无法处理 display:inline 值。

谢谢!

最佳答案

我建议您找到等效的 css 来解决,但如果您想要 jQuery 解决方案,那么就在这里。

var totalHeight;
$(".cat").each(functon(){
   totalHeight = 0;
   $(this).children(".layer").each(function(){
      totalHeight += $(this).height();
   })
   $(this).height(totalHeight);
});

关于javascript - jQuery:将 <section> 的高度设置为其所有子项的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928482/

相关文章:

javascript - Angular 2 : what expressions can we interpolate in template

javascript - 如何防止 jQuery prepend() 删除 HTML?

javascript - Google Chrome Extension - content_scripts 与 background_page js 文件通信

javascript - jQuery datepicker 上一个和下一个按钮不显示,如何在 css 中指向主题文件夹图像

javascript - 在循环内迭代函数的参数

php - 发布表单不适用于 jquery 字段

javascript - 如何制作响应式图像?

javascript - 创建滚动标记以显示网页中的重要位置

jquery - 我想知道什么是实际的 IE 条件语句来支持所有旧版 ie 以及最新版本 ie

html - <li> - 转移到布局中的新行