按 javascript 中的动态内容查找 div 的最大高度

Find max height of div by dynamic content in javascript

我有一个 div 并且不止一个数据通过 jstl 标签进入这个 div。我想找到最大内容 div 高度。 我见过一个 link 但它每次显示 20 时都处于警报状态。 element with the max height from a set of elements

JSP

     <div id="div-height" class='cat-product-name'>${i.name} </div>

JAVA脚本

  $(window).load(function () {
  var maxHeight = Math.max.apply(null, $("#div-height").map(function ()
                {
                    return $(this).height();
                }).get());
                alert(maxHeight);
});

我想找到 div 的最大高度并设置每个 div 的高度。

页面上的每个元素 ID 都必须是唯一的,即不能有多个具有

的元素
id="div-height"

尝试使用 class 代替 (class="div-height")。请注意,您还必须将 jQuery 选择器调整为

$(".div-height")

你可以试试这个:-

$(document).ready(function() {
  var maxHeight = -1;

  $('.cat-product-name').each(function() {
    maxHeight = maxHeight > $(this).height() ? maxHeight :     $(this).height();
 });

 $('.cat-product-name').each(function() {
   $(this).height(maxHeight);
 });
});

参考 - Use jQuery/CSS to find the tallest of all elements

现代解决这个问题的方法是使用 css flex-box 和 align-items: stretch; :

.container {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  
  width: 250px;
}

.container>div {
  flex: 0 0 100px;
  outline: 5px solid #888;
  padding: 10px;
}
<div class="container">
  <div>Small content</div>
  <div>This divs content needs more height than their siblings!</div>
  <div>Some more text</div>
  <div>Other text</div>
</div>