Calculate height of div's children using jQuery

I want to match parent's height with the total height of its' children, so the content does not flow out from parent's border. I'm using the following code :

 $("#leftcolumn").each(function(){
     totalHeight=totalHeight+$(this).height();
  });

Will it iterate through all the div's children? Sometimes, it works sometimes it doesn't.

Also, I tried following code, assuming it will consider all its children. But the result is strange and gives doubled height from the correct result.

 $("#leftcolumn > *").each(function(){
   totalHeight=totalHeight+$(this).height();
 });

Thanks in advance.

18
задан bwegs 13 July 2016 в 19:52
поделиться

2 ответа

Попробуйте так:

var totalHeight = 0;

$("#leftcolumn").children().each(function(){
    totalHeight = totalHeight + $(this).outerHeight(true);
});

http://api.jquery.com/outerHeight/ принимает поля , отступы и граничит с в вычислении, которое должно дать более надежный результат.

46
ответ дан 30 November 2019 в 06:07
поделиться
 $('#leftColumn').children().each(function(){
    var Totalheight += $(this).Height();
})var parentHeight = $('#leftColumn').Height();
if(parentHeight===TotalHeight)
{//Do the nasty part}
else
{//Do the Good part}
-1
ответ дан 30 November 2019 в 06:07
поделиться
Другие вопросы по тегам:

Похожие вопросы: