2016-12-28 99 views
-2

問題已修改。 對象對象是我的錯。使用JQuery獲取DIV的高度

我想檢測一些div的高度,然後將該值分配給另一個div。

但我寫的代碼並沒有給我一個正確的答案(我對Javascript/Jquery不太好)。

The CSS & HTML僅用於演示目的。

我想給我的.iframe類留下空間高度,但使用jquery/Javascript。

我的結構幾乎相似,並且containerwidth是流動的。

$(document).ready(function() { 
 
    var lhs = $('.section-updates').parent().outerHeight(); 
 
    console.log(lhs); 
 
    var rhs = $('.customer-focus').parent().height(lhs); 
 
    console.log(rhs); 
 
    var cfH = $('.customer-focus').outerHeight(); 
 
    $('.iframe').height(rhs - cfH); 
 
});
.section-updates { 
 
    height: 500px; 
 
    width: 50%; 
 
    background: purple; 
 
    float: left; 
 
} 
 
.customer-focus { 
 
    height: 200px; 
 
    background: yellow; 
 
    float: left; 
 
    width: 50%; 
 
} 
 
.iframe { 
 
    min-height: 100px; 
 
    background: tomato; 
 
    float: left; 
 
    width: 50%; 
 
} 
 
.container { 
 
    width: 95%; 
 
    height: 100%; 
 
    margin: 0 auto; 
 
    overflow: hidden; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="container"> 
 

 
    <div class="section-updates"></div> 
 
    <div class="customer-focus"></div> 
 
    <div class="iframe"></div> 
 

 
</div>

+2

jQuery的'高度(值)'函數返回一個jQuery對象,請參閱[文檔](http://api.jquery.com/height/#height2) –

+0

我認爲.height()是隻讀的,因此你的行啓動'var rhs'是不合法的。 –

+1

可以多個參數傳遞給'的console.log(「LHS --------:」,左圖);' – Satpal

回答

1

盡力勸慰這樣的..它會顯示對象內容

console.log(lhs); 
0

在行var rhs = $('.customer-focus').parent().height(parseInt(lhs));要設置,因爲你是路過的高度參數高度函數在設置值parseInt(lhs)後顯然會返回對象。使用像console.log("RHS -----",rhs)或使用console.log(JSON.stringify(lhs))或者如果你只需要使用高度rhs=$('.customer-focus').parent().height();

0

console.log,取代+。當您使用+它試圖Concat的所有值設置爲一個字符串,因爲lhs是它不能簡單地轉換成字符串的對象,這就是爲什麼它打印[object Object]