0

絶対配置の div を含む div コンテナー class="content" が繰り返されています。

.content{
   width: 960px;
   margin: 0 auto;
   padding-bottom: 20px;
   position: relative;
   overflow: auto;
}

 .article0{
   width: 300px;
   position: absolute;
}

 .article1{
   width: 230px;
   position: absolute;
   top: 80px;
}

 .article2{
   width: 230px;
   position: absolute;
}

 .article3{
   width: 230px;
   position: absolute;
   margin-bottom: 20px;
   top: 500px;
}

言われているように、コンテナ class="content" は同じページで何度も繰り返されています:

<div class="content">
<div class="article1"></div>
<div class="article2"></div>
<div class="article3"></div>
</div>

私がやろうとしているのは、クラス .content を持つ各コンテナーの最後の子の高さと上部の位置をコンテナーに与えることです。以下のコードを試してみましたが、結果はありません。コンテナーは同じ高さの値しか取得しません。最後のコンテナーまたは最初のコンテナーの最後の子からのみだと思いますか? わからない!

$(function(){
var $box = $('.content');
var lastChildPos = $(':last-child', $box).position().top;
var lastChildHeight = $(':last-child', $box).height();

$box.height(lastChildPos + lastChildHeight);

});
4

1 に答える 1

1

enter code here複数のインスタンスがある場合は、次のようなものが必要になります。

$('.content').each(function(){

    var lastChildPos = $(':last-child', $(this)).position().top;
    var lastChildHeight = $(':last-child', $(this)).height();

    $(this).height(lastChildPos + lastChildHeight);

});

このようにして、それをボックスの各インスタンスに適用し、$(this)各コンテナーに関連する変数を取得します。

注: :last-child は、親コンテナー内の型の最後の子を選択します。多分あなたは試してみたい:

var lastChildPos = $(this).find('div:last-child').position().top;
于 2012-10-24T14:05:53.823 に答える