How do I hide an HTML element before the page loads using jQuery

前端 未结 7 1276
独厮守ぢ
独厮守ぢ 2020-12-07 20:44

I have some JQuery code that shows or hides a div.

$(\"div#extraControls\").show();   // OR .hide()

I initially want the div to be not visi

相关标签:
7条回答
  • 2020-12-07 21:14

    Make a css class

    .hidden {
    display: none;
    }
    

    Add this to any element you don't want to be visible when loading the page. Then use $("div#extraControls").show(); to display it again.

    0 讨论(0)
  • 2020-12-07 21:16
    #toggleMe //Keep this in your .css file
    {
    display:none;
    visibility:inherit;
    background-color:#d2d8c9;    
    }
    
    
     <a href="#" onclick="return false;">Expand Me</a>
     ///this way u can make a link to act like a button too
    
     <div id="toggleMe">  //this will be hidden during the page load
      //your elements
     </div>
    
    ///if you decide to make it display on a click, follow below:
    <script type="text/javascript" src="jquery.js"> </script> //make sure u include jquery.js file in ur project
    <script type="text/javascript">
     $(document).ready(function () {
            $("a").click(function () {
                $('#toggleMe').toggle("slow"); //Animation effect
            });
        });
    </script>
    
    ///Now for every alternate click, it shows and hides, but during page load; its hidden.
    //Hope this helps you guys ...
    
    0 讨论(0)
  • 2020-12-07 21:19

    What you could do is insert a inline script tag at the end of the document, or immediately after the div with id "extraControls". Should be fire a little sooner than.

    <div id="extraControls">i want to be invisible!</div>
    <script type="text/javascript">$("div#extraControls").hide()</script>
    

    Of course you could do this server-side as well, but maybe there's a good reason you don't want to do that (like, have the controls visible if the browser does NOT support javascript).

    0 讨论(0)
  • 2020-12-07 21:20

    In CSS:

    #extraControls { display: none; }
    

    Or in HTML:

    <div id="extraControls" style="display: none;"></div>
    
    0 讨论(0)
  • 2020-12-07 21:26
    div.hidden
    {
       display: none
    }
    
    <div id="extraControls" class="hidden">
    </div>
    
    $(document).ready(function() {
        $("div#extraControls").removeClass("hidden");
    });
    
    0 讨论(0)
  • 2020-12-07 21:29

    The best way to load javascript function over page is loaded, is

    $(window).bind("load", function() {
       // code here
    });
    

    In your case

    div.hidden
    {
       display: none
    }
    
    <div id="extraControls" class="hidden">
    </div>
    
    $(window).bind("load", function() {
       $("div#extraControls").removeClass("hidden");
    });
    
    0 讨论(0)
提交回复
热议问题