2016-12-28 62 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]