jQuery div height calculate separately

笑着哭i 提交于 2019-12-11 07:51:47

问题


I have this code:

var maxHeight = 0;
$('div.height').each(function() { maxHeight = Math.max(maxHeight, $(this).height());      }).height(maxHeight);

This code calculate each box even in length. But I want each box calculate separately in length. How can i do this?

My HTML

<div class="height"> 
     <div class="overlay height"></div>
     Content
</div>

<div class="height"> 
     <div class="overlay height"></div>
     Content
</div>

回答1:


$('div.height').each(function() { 
    maxHeight = Math.max(maxHeight, $(this).height());    
    $(this).height(maxHeight);  
})



回答2:


I have figure it out myself and this is the solution:

$('div.height').each(function() { 
    hParent = $(this).height();
    hChild = $(this).find('div.overlay').height();
    maxHeight = Math.max(hParent, hChild);    
    $(this).height(maxHeight);
    $(this).find('div.overlay').css("height",(maxHeight-72)+"px");  
});



回答3:


In your current code you are setting everything to the CURRENT value of maxHeight instead of the FINAL value of maxHeight. If I understand that you intend to do correctly, then try separating it out as such:

var maxHeight = 0;

$('div.height').each(function() { maxHeight = Math.max(maxHeight, $(this).height()); });

$('div.height').height(maxHeight);

This will apply the final maxHeight to all divs.



来源:https://stackoverflow.com/questions/6508732/jquery-div-height-calculate-separately

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!