最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - hide div when page load - Stack Overflow

programmeradmin2浏览0评论

I have jquery issue, Kindly see my jquery code:

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

});

My .toggle_container is shown always its good, But I want to close it first time, when page load. Can you please provide me any solution? I dont want to use $(".toggle_container").hide(); function for this problem because when i click on href then .toggle_container should not hide, It should open.

Regards.

I have jquery issue, Kindly see my jquery code:

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

});

My .toggle_container is shown always its good, But I want to close it first time, when page load. Can you please provide me any solution? I dont want to use $(".toggle_container").hide(); function for this problem because when i click on href then .toggle_container should not hide, It should open.

Regards.

Share Improve this question edited May 23, 2011 at 8:03 Stanislav Ageev 7784 silver badges10 bronze badges asked May 23, 2011 at 7:36 RizRiz 711 gold badge1 silver badge2 bronze badges 1
  • in which cases you want it to be shown and in which hidden? – Stanislav Ageev Commented May 23, 2011 at 7:45
Add a comment  | 

3 Answers 3

Reset to default 16

You can just add attribute

style="display:none"

to your element .toggle_container.

Then on first call of $(document).ready it will be shown.

The full test example:

<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>

Note: there`s no $(".toggle_container").show(); on $(document).ready

remove the

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

from your $(document).ready function.

in html part add style="display:none" for the toggle_container div.

check the @HCK s reply. he is clearly mentioned it..

Once the document gets loaded, a alert box will be prompted "page loaded".

$(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. 
});
发布评论

评论列表(0)

  1. 暂无评论