简体   繁体   English

如何动态获取div的高度

[英]How to get the height of the div dynamically

i have 2 div's, left and right, the content in right div is setted dynamically so as its height, so i haven't specified its height in html, now what i want is the height of the left div should be equal to the right div, how to set the height of the left div as per the height of right div, using jquery, i tried 我有2个div,分别为左和右,右div中的内容是根据其高度动态设置的,所以我没有在html中指定其高度,现在我要的是左div的高度应等于右div,如何使用jquery根据右div的高度设置左div的高度,我尝试了

$("#leftDiv").height($("RightDiv").height());

this does not seems working, as i haven't specified the height of the right div in html. 这似乎不起作用,因为我没有在html中指定正确的div的高度。 is there any other wayout, apart from using tables. 除了使用表格外,还有其他任何出路。

尝试这个:

$("#leftDiv").height($("#RightDiv").get(0).scrollHeight);

Description 描述

Looks like your selector for RightDiv is not right or you forgot to wait while the DOM is loaded. 看起来您的RightDiv选择器不正确,或者您在加载DOM时忘记等待。

Sample 样品

Html HTML

<div id="leftDiv" style="border:1px solid red">left div</div>
<div id="RightDiv" style="height:100px; border:1px solid red">right div</div>

jQuery jQuery的

$(function() {
   $("#leftDiv").height($("#RightDiv").height());
})

More Information 更多信息

try 尝试

var firstHeight = $("#RightDiv").height() + 'px';
$("#leftDiv").css("height", firstHeight);
$(function() {
 if($("#RightDiv").height() > $("#leftDiv").height()){    
           $("#leftDiv").css("height", $("#RightDiv").height() + "px");
        } else {
           $("#rightDiv").css("height", $("#leftDiv").height() + "px");
       }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM