0

如何在每个多个 div 上设置相等的高度?

<div class="brands">
    <div class="left" style="height:50px;">different height 1 same group</div>
    <div class="right" style="height:50px;">different height 2 same group</div>
</div>
 <div class="brands">
    <div class="left" style="height:150px;">different height 3 same group</div>
    <div class="right" style="height:150px;">different height 4 same group</div>
</div>

非常感谢。

4

1 回答 1

1

如果你想要它们都一样:

$('div.brands').children('div').css('height', '100px');

或者,如果不同的需要不同的高度:

$('div.brands').eq(0).children('div').css('height', '50px');
$('div.brands').eq(1).children('div').css('height', '150px');

要将子 div 设置为给定品牌 div 中最高子 div 的高度:

var leftHeight = 0;
var rightHeight = 0;
$('div.brands').each(function() {
    leftHeight = $(this).children('div.left').height();
    rightHeight = $(this).children('div.right').height();
    if(leftHeight > rightHeight) {
        $(this).children('div.right').css('height', leftHeight + 'px');
    }
    else {
        $(this).children('div.left').css('height', rightHeight + 'px');
    }        
});
于 2013-04-25T12:25:36.793 回答