2010-08-10 100 views
4

我正在使用選項卡式功能發佈我的博客。如何實現div#latest-featuredhide()然後show()內容完全加載後返回?jQuery隱藏()div直到完全加載

$(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; 
    }); 
}); 

HTML

<ul class="tabs"> 
    <li><a href="#f1">Title 1</a></li> 
    <li><a href="#f2">Title 2</a></li> 
</ul> 
<div id="latest-featured"> 
    <div id="f1" class="tab_content"> 
    <p>Content for title 1</p> 
    <p>Why I want to hide the #latest-featured because.. when had image @ script here.. the tab_content will collapse all and break my design</p> 
    </div> 
    <div id="f2" class="tab_content"> 
    <p>Content for title 1</p> 
    <p>Why I want to hide the #latest-featured because.. when had image @ script here.. the tab_content will collapse all and break my design</p> 
    </div> 
</div> 

比如我有id爲#latest-featured一個div,我想隱藏它,直到內容滿載,然後再顯示回來一切都被加載後。

如何實現上面的當前代碼。

+0

你想去哪裏使用'#latest -featured'?我在你的代碼中看不到任何線索。 – Sarfraz 2010-08-10 18:40:11

+0

ok更新爲html – kampit 2010-08-10 18:47:42

回答

12

可能最好做這樣的事情:

<div id="latest-features" style="display:none;"></div> 

$(function() { 
    // do work 
    $("div#latest-featured").show(); 
} 

它通常不是一個好主意,默認隱藏任何東西,但這樣會滿足您的要求。

+0

感謝獵人..它的工作。如何做到一個好主意? – kampit 2010-08-10 18:54:53

+4

這個問題是如果你的javascript弄錯了或者瀏覽器不支持js這個項目永遠不會顯示。 – hunter 2010-08-10 19:14:53

0

爲什麼不直接設置div顯示:none直到你加載數據,然後刪除display:none ...有很多方法可以做我剛剛提到的btw(例如,jQuery .toggle(true ))

2

如果您希望它顯示加載後聲明該div的CSS display: none,然後在$(document).ready函數中調用$('div#latest-featured').show();

0

如果您希望它顯示一次加載聲明CSS顯示:無該div,然後在$(document).ready函數中調用$('div#latest-featured').show();

羅伯特回答了最好的,我會投它,但還不能...望着這個解決方案几個小時,這是最好的,我發現...

相關問題