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

V S*_* SH 2 javascript jquery jsp jstl

我有一个 div 并且不止一个数据通过 jstl 标签进入这个 div。我想找到最大内容 div 高度。我看到了一个链接,但它每次都显示在警报中 20. 具有一组元素中最大高度的元素

JSP

     <div id="div-height" class='cat-product-name'>${i.name} </div>
Run Code Online (Sandbox Code Playgroud)

爪哇脚本

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

在此处输入图片说明

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

khu*_*o29 6

你可以试试这个:-

$(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);
 });
});
Run Code Online (Sandbox Code Playgroud)

参考 -使用 jQuery/CSS 查找所有元素中最高的