2013-03-26 38 views
1

我正在寫一些jquery獲取2個div的高度並將另一個div設置爲組合高度。添加PX回到div高度

var height1 = $("#heightSelector2").height(); 
var height2 =$("#heightSelector3").height(); 
var maxHeight = height1 + height2; 
console.log(maxHeight); 

如何設置$(「#heighSelector1」)= maxHeight? 我知道我需要在那裏添加px,我也看到了一些例子,但沒有足夠清楚。

回答

1

這裏是另一種方式來做到這一點。

$("#heightSelector1").css("height",maxHeight + "px");

5
$("#heightSeletor1").height(maxHeight + "px") 

說實話,我不相信你需要添加px回來,但如果這樣做,只需使用+運營商將其追加。

+0

可以使用例如未經+ PX:$( 「#heightSeletor1」)高度(maxHeight);您也可以使用$(「#heightSeletor1」)。css(「height」,maxHeight +「px」);在這個例子中,你確實需要+「px」 – 2013-03-26 15:27:15

+0

@CatalinDeaconescu完全正確,我已經在我的文章中提到過。 – 2013-03-26 15:27:32

+1

對不起,埃文,我沒有看到更新之前,我發佈 – 2013-03-26 15:29:07

1

您無需添加PX

試試這個

$("#heightSeletor1").height(maxHeight) 

例子:$(selector).height(30);

see here

+0

爲什麼downvote可以解釋 – PSR 2013-03-26 15:27:24