一组元素中具有最大高度的元素 [英] element with the max height from a set of elements

查看:96
本文介绍了一组元素中具有最大高度的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组div元素.在jQuery中,我希望能够找到具有最大高度以及div的高度的div.例如:

I have a set of div elements. In jQuery, I would like to be able to find out the div with the maximum height and also the height of that div. For instance:

<div>
    <div class="panel">
      Line 1
      Line 2
    </div>
    <div class="panel">
      Line 1<br/>
      Line 2<br/>
      Line 3<br/>
      Line 4<br/>
    </div>
    <div class="panel">
      Line 1
    </div>
    <div class="panel">
      Line 1
      Line 2
    </div>
</div>

通过查看上面的内容,我们知道第二个div(具有4行)具有最大的高度.我如何找到这个?有人可以帮忙吗?

By looking at the above, we know that the 2nd div (with 4 Lines) has the maximum height of all. How do I find this out? Could someone please help?

到目前为止,我已经尝试过:

So far I've tried:

$("div.panel").height(),它返回第一个div的高度.

$("div.panel").height() which returns the height of the 1st div.

推荐答案

使用 .map() Math.max .

var maxHeight = Math.max.apply(null, $("div.panel").map(function ()
{
    return $(this).height();
}).get());


如果这让人难以理解,那可能会更清楚:


If that's confusing to read, this might be clearer:

var heights = $("div.panel").map(function ()
    {
        return $(this).height();
    }).get();

maxHeight = Math.max.apply(null, heights);

这篇关于一组元素中具有最大高度的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆