2011-11-22 180 views
1

我已將JQuery選項卡併入我的網站,但是我希望我的選項卡可以將高度展開並收縮到內容中。例如,當我的第一個標籤擴展到內容的高度時,當我轉到第二個標籤時,它停留在第一個標籤的高度,即使它的內容少得多。對不起,如果這是令人困惑。如何將標籤高度設置爲內容高度

下面是標籤代碼:

<script type="text/javascript"> 

    $(document).ready(function() { 

//Default Action 
$(".tab_content").hide(); //Hide all content 
$("ul.tabs li:first").addClass("active").show(); //Activate first tab 
$(".tab_content:first").show(); //Show first tab content 

//On Click Event 
$("ul.tabs li").click(function() { 
    $("ul.tabs li").removeClass("active"); //Remove any "active" class 
    $(this).addClass("active"); //Add "active" class to selected tab 
    $(".tab_content").hide(); //Hide all tab content 
    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content 
    $(activeTab).fadeIn(); //Fade in the active content 
    return false; 
}); 

    }); 
    </script> 

這裏是一段代碼,我用周圍div包裝,以控制div的高度:

<script type="text/javascript"> 
$(function(){ 
var height = $("#content").height(); 
$(".tab_container").height(height); 
}); 
</script> 

如果您需要更多的信息讓我知道,我會提供。

+0

您目前是否有調用該功能或​​僅在頁面加載時執行? –

回答

0

我認爲包裝div會根據內容的高度展開和收縮。你確定高度沒有設置在某個地方嗎? JSFiddle中的示例的鏈接將會很有幫助。

2

它似乎最初工作正常,因爲您有頁面加載代碼,但一旦內容更改高度,您沒有任何回憶該功能的東西。我將圍繞該功能打包

$("ul.tabs li").click(function() { 
     var contentHeight = $("#content").height(); 
     $(".tab_container").height(contentHeight); 
}); 
+0

嗨,你的代碼在刷新高度方面起作用,但是它將其他div的高度設置爲內容高度,我怎樣才能將其更改爲最小高度。在添加此代碼之前,即使我在第一個標籤中沒有內容,該標籤仍然具有最小高度。有任何想法嗎?謝謝 –

+0

你總是可以說,如果contentHeight <500比contentHeight = 500,這樣你的最低高度將是500px。 –

相關問題