javascript - 匹配元素的高度

标签 javascript jquery html css dom

我有几个 <div>元素彼此相邻,我想将它们的高度与最高的元素相匹配。目前我是这样做的:

jQuery(function($) {
    var maxHeight = 0;
    $( ".services-area").each (function (index ) {
        if (maxHeight < $( this ).height()) {
            maxHeight = $( this ).height();
        }
    });

    $( ".services-area").each (function (index ) {
        $( this ).height(maxHeight);
    });
})

有没有更好的方法来实现这个结果?

最佳答案

就我个人而言,我这样做:

$.fn.sameHeight = function(){
    var max = 0;
    this.each(function(){
        max = Math.max($(this).height(),max);
    });
    return this.height(max);
};

然后我可以随时调用它

$('.column').sameHeight();

请参阅此处的实时示例 http://jsfiddle.net/Panomosh/t6xsjef7/

关于javascript - 匹配元素的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30375336/

相关文章:

javascript - 需要更好地理解这段代码

jquery - 如何根据值获取选择选项

javascript - 如何在 x 轴上设置固定金额,以便显示 future 的时间?

javascript - 为什么仅仅引用Bootstrap css和js来使用Bootstrap类还不够?

javascript - Lightslider 响应高度被 chop

javascript - Rails 不会在 native JavaScript Vanilla 中通过 Ajax 执行 create.js

javascript - 为什么我的 'for' 循环函数不起作用?

javascript - 根据动态键数组从数组返回值

javascript - 如何使用 jQuery 和 CSS 在底层以精确位置获取 DIV 的背景

javascript - 在 d3 中为 datalist 创建元素