【问题标题】:jQuery - getting the height of the highest elementjQuery - 获取最高元素的高度
【发布时间】:2014-09-14 07:40:13
【问题描述】:

假设我有一堆具有不同动态高度的未排序列表(取决于列表中有多少元素)。 如何获得最高的高度?

我已经尝试在this 帖子的帮助下实现目标。

这就是我一直在尝试的(红色背景色只是为了看看它是否有效)

    $.fn.getMax = function() {
     /* create array of heights*/
    var heights = $(this).map(function(i, e) {
        return $(e).height();
    }).get();
    /* get max height*/
    var max = Math.max.apply(this, heights);
    /* get index of max in array*/
    var pos = $.inArray(max, heights)
    /* return element with proper index*/
    return this.eq(pos);
};

$('.navi-main li').getMax().css({backgroundColor: "red"});

【问题讨论】:

  • 发布您的 html 代码...
  • 请发布您到目前为止尝试过的内容。可能是您没有正确遵循参考帖子,或者可能有一些错误。
  • 对不起,刚刚添加了一些代码

标签: jquery arrays height


【解决方案1】:

您可以尝试以下方法:

var max = -1;
$('.navi-main li').each(function() {
    max = Math.max(max, $(this).height());
});
alert(max);

或作为单行:

var max = Math.max.apply(Math, $('.navi-main li').map(function() { return $(this).height(); }));
alert(max);

;

【讨论】:

    猜你喜欢
    • 2011-08-05
    • 2010-11-07
    • 2012-05-01
    • 1970-01-01
    • 2010-10-06
    • 2012-05-31
    • 2016-04-27
    相关资源
    最近更新 更多