javascript - 从最高的 div 获取高度并将其应用于其他 div

标签 javascript jquery height

基本上

我有几个具有灵活高度和固定填充的 div,因此很难让它们都具有相同的高度,

我正在尝试这样:

$(document).ready(function(){
    $('.LevelCnt_2,.LevelCnt_2 .content,.LevelCnt_1,.LevelCnt_1 .content').outerHeight( $('header:eq(0)').outerHeight() );


    console.log($('header:eq(0)').outerHeight(true));
});

但问题是标题并不总是最高的,所以我需要

  • 检查哪个更高(考虑到有多个 .content 元素)

  • 应用外部高度 对他们所有人

但我找不到好的/漂亮的方法(有一个解决方案,但我需要很多 if 和变量)来做到这一点

有什么线索吗?

-编辑-

在等待的时候我想到了这个

$(document).ready(function(){

    var height = 0;
    $('.LevelCnt_2,.LevelCnt_2 .content,.LevelCnt_1,.LevelCnt_1 .content,header').each(function(){
         if($(this).outerHeight() > height){
            height = $(this).outerHeight();
         }
    });
    $('.LevelCnt_2,.LevelCnt_2 .content,.LevelCnt_1,.LevelCnt_1 .content,header').each(function(){

             $(this).outerHeight(height);

    });

    console.log('highest height is '+height); //it doesn't output the highest value
});

但大多数 div 都在 display:none 中,这是问题所在吗?

最佳答案

不是添加 display none,而是给它们“hide”类并使用它。

$(function(){
    var height = 0;
    $('.hide').show();
    $('header').each(function(){
        height = Math.max( height, $(this).outerHeight() )
    });
    $('.hide').hide();
    $('.LevelCnt_2,.LevelCnt_2 .content,.LevelCnt_1,.LevelCnt_1 .content').outerHeight(height);
    console.log($('header:eq(0)').outerHeight(true));
});

关于javascript - 从最高的 div 获取高度并将其应用于其他 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8153804/

相关文章:

Javascript/jQuery : Why different result for event. 目标使用 <a> 标签对比。 console.log 中的其他元素,注意 $(this) 的 alert()

javascript - 如何将切换按钮集成到 Bootstrap 3.3.7?

javascript - 关闭模态对话框屏幕

php - 想要记录点击页面上的链接所花费的时间

javascript - 如何解决 "Window.onDomReady is not a function"问题?

jquery - 如何在 html 和 css 中将 div 居中,始终保持纵横比,并填充最大屏幕宽度/高度?

Javascript/jQuery - 验证表单所需的最少注释数

javascript - 确定 Javascript 中元素的大小

html - CSS:如何定义一个带有无限垂直边框且初始高度为 100% 的居中 div?

css - Div 未调整大小以适合内容