使用jquery和动态内容检测高度

时间:2014-01-23 20:54:44

标签: jquery css cakephp

我有一个必须垂直对齐的div,div中的文本是动态生成的,所以我需要使用生成的内容获取div大小,然后将其设置为其他div,以便它可以垂直对齐。 / p>

这是我的标记

<?php foreach ($necesidades as $necesidad): ?>
    <div class="guia-wrap">
         <div class="tit">
              <div><?php echo $necesidad->getTituloByPais($pais) ?></div>
         </div>
         <div class="desa">
              <div><?php echo html_entity_decode($necesidad->getDescripcion()) ?></div>
         </div>

         <script>
             $(document).ready(function(){
                 altura = $('.desa').height();
                 $(".tit").css("height", altura);
                 $(".tab_content").hide();
             });
         </script> 
    </div>
<?php endforeach; ?>

所以,我得到.desa的高度并将其设置为.tit。但是当生成几个.guia-wraps时,我不知道如何对每个.desa的高度进行geat并将它们设置为自己的.tit

这是css

.tit { 
    display: table; 
    float: left; 
    width: 213px; 
    border-right:1px solid #dedede; 
    height: 100%; 
    font-family: arial;
    font-size: 14px; 
    color: #666666; 
    background: #f6f6f6;
 }
.tit div {
    display: table-cell; 
    vertical-align: middle; 
    padding: 15px;
}
.desa { 
    display: table;
    float: right; 
    width: 476px; 
    font-family: arial; 
    font-size: 12px; 
    height: 100%; 
    color: #959595; 
}
.desa div {
    display: table-cell; 
    vertical-align: middle; 
    padding: 15px;
}

希望它足够清楚。谢谢。

1 个答案:

答案 0 :(得分:0)

使用Jquery,您可以使用each()这样做:

$(document).ready(function(){
   $('.guia-wrap').each(function(){
      var h = $('.desa',this).css('height');
      $('.tit', this).css('height',h)
   })
})