开发者

Pick the biggest height of all elements

开发者 https://www.devze.com 2023-01-21 22:58 出处:网络
This is my code. v=dom.find(\"ul:first\").outerHeight(); // the height of the first li element // instead of taking the height of the first li element, it should loop through all

This is my code.

    v=dom.find("ul:first").outerHeight(); // the height of the first li element
            // instead of taking the height of the first li element, it should loop through all
            // li elements and set v to the element with the biggest height

The comment in the middle of the code pretty much explains everything. I need to loop through all li elements and take the biggest heig开发者_StackOverflow社区ht, instead of taking the height of the first element.


var v = 0;
// ...
dom.find('ul:first').children('li').each(function() {
    var height = $(this).outerHeight();
    v = height > v ? height : v;
});


you can use the jquery .each() method to loop through each element, and the .height() method to determine the height of the element. to determine the greatest height, declare a variable, maxheight=0, and if the element height is greater than maxheight, set the maxheight to the element height.

var maxheight = 0;
$('ul').each(function(index) {
     if($(this).outerHeight() > maxheight) maxheight = $(this).outerHeight();
});


var el, max = 0;
$("ul").each(function(){ 
  var height = $(this).outerHeight();
  if (height > max) {
    el  = this;
    max = height;
  }
});


var maxHeight = Math.apply(null, $('ul:first > li').map(function() { 
  return $(this).outerHeight();
}));

This works as well, just without the extra var.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号