【问题标题】:How to set new height of class="container" to end of data?如何将 class="container" 的新高度设置为数据末尾?
【发布时间】:2014-02-05 15:58:09
【问题描述】:

如何将 class="container" 的新高度设置为数据末尾?

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
<style type="text/css">
    .container {
    background: green;
    position: relative;
}
.absolute1 {
    position: absolute;
    background: red;
    float: left;
    width: 200px;
    height: 200px;
}
.absolute2 {
    position: absolute;
    background: red;
    float: left;
    left: 210px;
    width: 200px;
    height: 150px;
}
  </style>

<script type="text/javascript">//<![CDATA[ 
$(window).load(function(){
$( document ).ready(function() {
    $( ".container" ).each(function() {
        var newHeight = 0, $this = $( this );
        $.each( $this.children(), function() {
            newHeight += $( this ).height();
        });
        $this.height( newHeight );
    });
});
});//]]>  

</script>

<div class="container">
       <div class="absolute1">
           Testing absoluteTesting absoluteTesting absoluteTesting absoluteTesting absolute<br />
           Even more testing absolven more testing absolven more testing absolven more testing absolven more testing absolute<br />
       </div>    
           <div class="absolute2">
           Testing absoluteTesting absoluteTesting absoluteTesting absoluteTesting absolute<br />
           Even more testing absolven more testing absolven more testing absolven more testing absolven more testing absolute<br />
       </div>   
</div>

------------------footer text------------------

【问题讨论】:

  • “数据结束”是什么意思?
  • 旁注:你不需要$(window).load() AND $(document).ready()

标签: javascript jquery css height


【解决方案1】:

使用jquery .outerHeight()函数

//.outerHeight()计算中始终包含上下内边距和边框;如果 includeMargin 参数设置为 true,则还包括边距(顶部和底部)。

你将获得元素的新高度,然后使用 jquery 将此值设置为元素 css

【讨论】:

  • 你有演示吗?
猜你喜欢
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
相关资源
最近更新 更多