Get the sum of the outerHeight of all elements of the same class

前端 未结 9 1648
Happy的楠姐
Happy的楠姐 2021-01-03 23:46

I think this is a pretty straightforward problem but...

var outerHeight = $(\'.profile\').outerHeight();
$(\"#total-height\").text(outerHeight + \'px\');
         


        
9条回答
  •  感动是毒
    2021-01-04 00:05

    jQuery functions that don't return a jQuery object operate only on the first member of a list.

    If you want to iterate over all .profile elements, you can use .each()

    var totalHeight = 0;
    $('.profile').each(function(i, e) {
        totalHeight += $(e).outerHeight();
    });
    

提交回复
热议问题