【问题标题】:jQuery hide() div until fully loadedjQuery hide() div 直到完全加载
【发布时间】:2010-08-10 18:37:35
【问题描述】:

我正在为我的博客使用标签式精选帖子。如何实现div#latest-featured将hide()然后show()在内容完全加载后返回?

$(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,我想隐藏它直到内容完全加载,然后在加载完所有内容后将其显示回来。

如何实现到上面的当前代码。

【问题讨论】:

  • 你想在哪里使用#latest-featured?我在您的代码中看不到任何线索。

标签: jquery css show-hide


【解决方案1】:

最好这样做:

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

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

默认隐藏任何内容通常不是一个好主意,但这会满足您的要求。

【讨论】:

  • 问题是如果您的 javascript 出现问题或浏览器不支持 js,则该项目将永远不会显示。
【解决方案2】:

如果您希望它在加载后显示,请为该 div 声明 CSS display: none,然后在 $(document).ready 函数中调用 $('div#latest-featured').show();。

【讨论】:

    【解决方案3】:

    如果您希望它在加载后显示,请声明该 div 的 CSS display: none,然后在您的 $(document).ready 函数中调用 $('div#latest-featured').show();。

    罗伯特回答得最好我会投票但还不能...寻找这个解决方案几个小时,这是我找到的最好的...

    【讨论】:

      【解决方案4】:

      为什么在加载数据之前不将 div 设置为 display:none,然后删除 display:none ...顺便说一句,有很多方法可以做我刚才提到的事情(例如,jQuery .toggle(true) )

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 2020-08-20
        • 2015-11-19
        • 2020-07-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多