2012-10-29 91 views
0

我在定位多個div元素時遇到問題。我有三個不同高度的div元素。但在「div 1」中是下滑菜單,因此「div 1」的高度會發生變化。我想知道,我怎樣才能自動設置所有三個div元素的高度相同。如何自動設置DIV元素的高度

+0

,哪些事件改變這些高度? –

+1

需要更多的代碼,也許是[jsFiddle?](http://www.jsfiddle.net) – SpYk3HH

回答

0

聽起來像是在爲一個div的滑蓋向下回調函數,你只需要:

// grab the new hight 
var newHeight = $(this).outerHeight(); 

// set it to the other two divs 
$('.otherDivs').height(newHeight); 
1

這個jQuery插件將設置每個元素的高度了一套的高度最高的一個:

// Sets the height of a set to the height of the tallest member. 
$.fn.sizeToTallest = function() { 
    var tallest = 0; 
    this.each(function() { 
     var h = $(this).height(); 
     if (h > tallest) tallest = h; 
    }); 
    this.height(tallest); 
    return this; 
}; 

使用方法如下:

$("myDivs").sizeToTallest(); 
0
$height = "50px"; // or whatever height you wish 


$("div").each(function(e) 
{ 
    $(this).css("height",$height); 
}); 

的每個函數會遍歷你的div,然後設置其高度,以50px

0
$('#div1').slideDown(400, function() { 
    $('#div2, #div3').height($(this).height()); 
});