简体   繁体   English

计算每格的总高度

[英]Calculate total height per div

My HTML code looks as follow 我的HTML代码如下

<ul>
<li id="slide-page<?php echo $i; ?>" class="dragend-page page<?php echo $i; ?>">
    <div class=“mt_row-fluid”></div>
    <div class=“mt_row-fluid”></div>
</li>
<li id="slide-page<?php echo $i; ?>" class="dragend-page page<?php echo $i; ?>">
    <div class=“mt_row-fluid”></div>
</li>
<li id="slide-page<?php echo $i; ?>" class="dragend-page page<?php echo $i; ?>">
    <div class=“mt_row-fluid”></div>
    <div class=“mt_row-fluid”></div>
    <div class=“mt_row-fluid”></div>
    <div class=“mt_row-fluid”></div>
</li>
</ul>

In some of the li elements, there are more divs with class 'mt_row-fluid' than in other li elements. 在某些li元素中,“ mt_row-fluid”类的div比其他li元素中的更多。 What I'd like to do is calculate the total height of all divs per li element. 我想做的是计算每个li元素的所有div的总高度。 When doing so, I can check whether they are larger than the screen, and trigger a button if needed to make clear the user can scroll down. 这样做时,我可以检查它们是否大于屏幕,并在需要时触发按钮以使用户可以向下滚动。

I've tried everything I could think off, but without any luck. 我已经尽力想了一切,但是没有任何运气。 Hopefully you could help me out. 希望你能帮助我。

   function totalHeight(){
var myVar = "#slide-page" + <?= json_encode($i) ?>;
var getHeight = 0;

$(this).find(myVar + ' .mt_row-fluid').each(function(){
    getHeight += $(this).height();
});    

console.log(getHeight);
};

totalHeight();

The code above is one of the solutions I've tried without any succes. 上面的代码是我尝试的没有成功的解决方案之一。 Perhaps there is a completely different way to get to what I want, but please let me know if that's the case. 也许有一种完全不同的方式来达到我想要的目的,但是如果是这种情况,请告诉我。

You can Try this : 您可以尝试:

  $(function(){
    var liElements = $("li");
    $.each(liElements, function (i) {
      var widthOfAllDivInLi =0;  
      var divsMtRowFluid=$(this).find("div");
        $.each(divsMtRowFluid, function (){
          widthOfAllDivInLi+= this.offsetWidth;      
        });
        console.log("width all div of Li number "+(i+1)+ ":" +widthOfAllDivInLi)  
    });
  })

And test it yourself : http://jsfiddle.net/m4jeL2uf/4/ 并自己测试: http : //jsfiddle.net/m4jeL2uf/4/

If you have any question about this answer i'm here to help. 如果您对此答案有任何疑问,我们将为您提供帮助。

Best regards 最好的祝福

you have juste to change the "find()" function parameter "div" by ".nameOFYourClass", in your case ".mt_row-fluid" : 您只需通过“ .nameOFYourClass”更改“ find()”函数参数“ div”,在您的情况下为“ .mt_row-fluid”:

$(function(){
var liElements = $("li");
$.each(liElements, function (i) {
  var widthOfAllDivInLi =0;  
  var divsMtRowFluid=$(this).find(".mt_row-fluid");
    $.each(divsMtRowFluid, function (){
      widthOfAllDivInLi+= this.offsetWidth;      
    });
    console.log("width all div of Li number "+(i+1)+ ":" +widthOfAllDivInLi)  
});
})

You can test it again : http://jsfiddle.net/m4jeL2uf/7/ 您可以再次对其进行测试: http : //jsfiddle.net/m4jeL2uf/7/

I invite you to read more about Jquery Selectors, it will be very helpful to understand JQuery traversing ( to find HTML elements ) 我邀请您阅读有关Jquery选择器的更多信息,这对理解JQuery遍历(查找HTML元素)将非常有帮助。

Best regards 最好的祝福

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM