2013-02-22 79 views
1

我有一個評論列表,其中包含絕對定位的div中的內容。這是設計的一部分。它們位於彈出框中,該框設置爲顯示:無,然後使用單擊事件淡入。找到絕對定位的子元素的高度並將其高度設置爲其父親

我遇到的問題是我需要獲取內部絕對內容的高度並將該高度設置爲其父級,並且似乎無法使其工作。每個項目可以是任意長度,因爲它們是評論。

我在這裏做了一個小提琴來展示我有什麼樣的結構。 http://jsfiddle.net/ZTDyA/

這是結構

<div class="hidden"> 
    <ul class="list"> 
     <li> 
      <div class="inner">there is some content here</div> 
      <div class="base">This is a div under the .inner div</div> 
     </li> 
     <li> 
      <div class="inner">For the thing I have made this has to be positioned absolutely, but the content could be of any length so I need the parent to each of these inners to be set to the height of the inner div.</div> 
      <div class="base">This is a div under the .inner div</div> 
     </li> 
    </ul> 
</div> 
+0

爲什麼你就不能刪除'位置? http://jsfiddle.net/dfsq/ZTDyA/2/ – dfsq 2013-02-22 13:33:32

+0

由於設計原因,評論必須位於懸停時可見的另一圖層之上。 – 2013-02-22 13:34:59

回答

2

當然,你可以做這樣的事情:

$('.list li').each(function() { 
    $(this).height($('.inner', this).height()); 
}); 

的jsfiddle:http://jsfiddle.net/ZTDyA/1/

我不明白的是爲什麼它絕對定位。如果有的話,你可能想要定位李的絕對。也許如果你詳細說明,我們可以提供更好的解決方案。像這樣設置高度(通過js)是不好的練習。

+0

這一切都與這個評論區的設計有關,基本上我有一個div位於懸停時顯示的評論內容之下。這完全解決了它,謝謝你。 – 2013-02-22 13:39:24

0

檢查:從`.inner` absolute`:

$('button').on('click', function() {  
var hei= $('.inner').height(); 
$('.inner').closest('li').height(hei); 
}); 
相關問題