最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Find max height of div by dynamic content in javascript - Stack Overflow

programmeradmin6浏览0评论

I have one div and more than one data es into this div by jstl tag. I want to find max content div height. I have seen one link but it is show in alert every time 20. element with the max height from a set of elements

JSP

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

JAVA SCRIPT

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

I want to find max height of div and set this height of every div.

I have one div and more than one data es into this div by jstl tag. I want to find max content div height. I have seen one link but it is show in alert every time 20. element with the max height from a set of elements

JSP

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

JAVA SCRIPT

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

I want to find max height of div and set this height of every div.

Share Improve this question edited May 23, 2017 at 12:07 CommunityBot 11 silver badge asked Feb 20, 2016 at 9:25 Varun SharmaVarun Sharma 4,84213 gold badges55 silver badges105 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You can try this:-

$(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);
 });
});

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

Each element id on the page must be unique, i.e. you can't have multiple elements with

id="div-height"

Try using class instead (class="div-height"). Note you'll have to adjust your jQuery selector as well to

$(".div-height")

A modern take to this problem is the usage of css flex-box with 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>

发布评论

评论列表(0)

  1. 暂无评论