簡體   English   中英

選擇所有元素中最大的高度

[英]Pick the biggest height of all elements

這是我的代碼。

    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

代碼中間的注釋幾乎可以解釋所有內容。 我需要遍歷所有li元素並采用最大高度,而不是第一個元素的高度。

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

您可以使用jquery .each()方法遍歷每個元素,並使用.height()方法確定元素的高度。 要確定最大高度,請聲明變量maxheight = 0,如果元素高度大於maxheight,則將maxheight設置為元素高度。

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

即使沒有多余的var,它也可以正常工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM