2016-02-11 61 views
0

我有兩個元素。兩者都是動態的。在Javascript或jQuery中,我想抓住這兩個元素的高度,並設置這兩個元素組合高度的元素的高度。獲取兩個元素的高度,將另一個元素設置爲該高度

我知道可以設定的與另一個元件:

$("#canvas-wrapper").height($("main.hero").height()); 

我試圖包括其中高度被抓住諸如在區域中的第二元件:

$("div#canvas-wrapper").height($("header", "main.hero").height()); 

但ISN不爲我工作。我需要將這些元素中的兩個放入數組中嗎?

回答

0

使用這兩種選擇不給你結合的高度,你必須做出獨立的呼籲每個:

$("div#canvas-wrapper").height($("header").height() + $("main.hero").height()); 
0

您的代碼在語法上是錯誤的。你必須單獨找到每個高度,並將它們加在一起以獲得高度。

得到總的高度 -

var totHeight = $("header").height() + $("main.hero").height();

要設置

$("div#canvas-wrapper").height(totHeight);

或者兩者都是一句話這樣

$("div#canvas-wrapper").height($("header").height() + $("main.hero").height());

相關問題