Jquery函数找出<li>的最大高度

ami*_*mit 13 javascript jquery

我想找出了给予<li>li具有最大高度?我该怎么写这样的功能?

如果有n <li>个不同大小,内容和高度的元素.我想弄清楚最大<li>元素的最大高度.

Igo*_*mov 29

试试这个:

var max = -1;
$("li").each(function() {
    var h = $(this).height(); 
    max = h > max ? h : max;
});
alert(max);
Run Code Online (Sandbox Code Playgroud)

  • 或者`max = Math.max(max,$(this).height());`,任何人(除了我)都应该发现它更具可读性. (10认同)

Dog*_*ert 20

如果你想要一个衬垫

var max = Math.max.apply(Math, $("li").map(function() { return $(this).height(); }))
Run Code Online (Sandbox Code Playgroud)


Sot*_*ris 5

var liMaxHeight = -1;
var node;
$("li").each(function(index) {
    if ($(this).outerHeight() > liMaxHeight) {
        liMaxHeight = $(this).outerHeight();
        node = index;
    }
});
alert("li with index " + node + " has height " + liMaxHeight);
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/QUycm/

.outerHeight()包括填充和边框.如果不想包括填充和边框使用height()