2014-01-12 78 views
0

我正在嘗試使'.caption'的高度與'.post'相同。使元素的高度等於div

問題是我有很多不同高度的'.post'div。

這是我有:

$(document).ready(function(){ 
    var postH = $('.post').height(); 
    $('.caption').css("height", postH); 
}); 

它的工作原理,但僅適用於第一個元素,元素的其餘部分仍與第一「.caption」的高度。

謝謝!

編輯:

HTML結構:

<div class="content"> 

    <div class="entry"> 
     <div class="post">...</div> 
     <div class="caption">...</div> 
    </div> 

    <div class="entry"> 
     <div class="post">...</div> 
     <div class="caption">...</div> 
    </div> 

    <div class="entry"> 
     <div class="post">...</div> 
     <div class="caption">...</div> 
    </div> 

    <div class="entry"> 
     <div class="post">...</div> 
     <div class="caption">...</div> 
    </div> 

</div> 

UPDATE:

我終於找到了一個解決方案:

$('.entry').each(function() { 
    $(this).height($(this).find('.post').height() + 80); 
}); 

我設置 '.entry' 的溢出是隱。該「+ 80」是因爲「.POST」具有40

+0

使用next px'添加'它。 – Ani

+1

不要使用高度作爲CSS參數。使用'$('。caption')。height($('。post')。height());' – Gasim

+0

謝謝,但它仍然無效..高度僅適用於第一個,我不喜歡不知道爲什麼 – user3052619

回答

4

填充如果我沒有理解好了,你有很多崗位,很多字幕,所以它應該看起來像

$('.post').each(function() { 
    $(this).closest('.caption').css('height', $(this).height()); 
}); 

這可能不是與closest(取決於你的結構)一起工作,但你已經明白了。

編輯

從您的更新

代替closest

$('.post').each(function() { 
    $(this).next('.caption').height($(this).height()); 
}); 
+0

'.caption'不是'.post'的孩子,是否應該讓它工作? – user3052619

+0

@ user3052619它不需要,但是沒有看到你的結構的HTML片段,很難猜測 –

+0

@ user3052619你不需要改變你的DOM,但我們必須知道它找到正確的功能(可能或可能不是「最接近」)。嘗試把樣品放在jsfiddle上... –