位置相对内容计算高度

position relative content calculate height

本文关键字:计算 高度 相对 位置      更新时间:2023-09-26

我有一个位置相对容器,它有很多绝对位置div。我想计算我的相对容器的绝对高度div。我如何用css或jquery做到这一点?我看不到容器的红色背景…

<!DOCTYPE html>
<html>
<body>
<div id="container" style="position:relative;overflow:hidden;width:300px;background-color:red;">
    <div style="position:absolute;top:0px;left:0px;width:100px;">
        Lorem ipsum
    </div>  
    <div style="position:absolute;top:0px;width:100px;left:100px;">
        Lorem Ipsum
    </div>
</div>
</body>
</html>

如果div都是水平定位(彼此相邻)和宽度没有左或右边距(通过查看你的代码,我认为条件是匹配的),那么你可以这样做:http://jsfiddle.net/U6RXB/1/

var width=0;
$('#container').children('div').each(function(){
    width=width+$(this).width();
});
$('#container').width(width);

如果我提到的任何条件与您的代码不匹配,请通知我,我会再次修复。