簡體   English   中英

頁面加載時隱藏div

[英]hide div when page load

我有jquery問題,請看我的jquery代碼:

$(document).ready(function(){

    $(".toggle_container").show();

    $("h2.trigger").toggle(function(){
            $(this).addClass("active");
            }, function () {
            $(this).removeClass("active");
    });

    $("h2.trigger").click(function(){
            $(this).next(".toggle_container").slideToggle("slow,");
    });

});

我的.toggle_container總是很好用,但是我想在頁面加載時第一次關閉它。 能幫我解決一下嗎? 我不想使用$(“。toggle_container”)。hide(); 這個問題的功能,因為當我點擊href然后.toggle_container不應該隱藏,它應該打開。

問候。

你可以添加屬性

style="display:none"

你的元素.toggle_container。

然后在第一次調用$(document).ready時會顯示。

完整的測試示例:

<html>
<head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
      $("h2.trigger").click(function(){
        $(this).next(".toggle_container").slideToggle("slow,");
      });
    });
  </script>
</head>
<body>
  <h2 class="trigger">Toggle</h2>
  <div class="toggle_container" style="display:none">Container</div>
</body>
</html>

注意:沒有$(“。toggle_container”)。show(); 在$(文件).ready

去除

$(".toggle_container").show();

來自你的$(document).ready函數。

在html部分中為toggle_container div添加style="display:none"

查看@HCK的回復。 他明確提到了..

文檔加載后,將提示“頁面加載”警告框。

$(document).ready(function(){    
    alert('page loaded');  // alert to confirm the page is loaded    
    $('.divClassName').hide(); //enter the class or id of the particular html element which you wish to hide. 
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM