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>
&lt/div>

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?

到目前为止,我已经try 过:

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

推荐答案

Use .map() and 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);

Jquery相关问答推荐

将div中的下一个2个标题分组

点击和touch 事件之间的jQuery冲突解决方法

jquery如何判断ajax调用的响应类型

使用淡入淡出和追加

使用 jQuery 和 HTML 导出为 CSV

JavaScript 中的循环计时器

为什么 jquery fadeIn() 不能与 .html() 一起使用?

用 jQuery 模拟按键

如何使用 jQuery UI Resizable 仅水平或垂直调整大小?

如何使div全屏?

Angular.js 在多个元素中重复

jquery变量语法

好处和.在本地托管 jQuery 的trap

我如何知道 jQuery 是否有待处理的 Ajax 请求?

Rails 无法正确解码来自 jQuery 的 JSON(数组变成带有整数键的散列)

jquery,id内类的 Select 器

jQuery: Select 不为空的数据属性?

CORS - 如何预检httprequest?

Drop 事件未在 chrome 中触发

Bootstrap:在 Modal 中打开另一个 Modal