jquery shows several ways to hide a div

Source: Internet
Author: User

1, $ ("#demo"). attr ("style", "display:none;"); /Hide Div

$ ("#demo"). attr ("style", "display:block;"); /Show Div

2, $ ("#demo"). CSS ("display", "none");//Hide Div

$ ("#demo"). CSS ("Display", "block");//Display Div

3, $ ("#demo"). Hide ();//Hidden Div

$ ("#demo"). Show ();//Display Div

4, $ ("#demo"). Toggle (//dynamic display and hide

function () {

$ (this). attr ("style", "display:none;"); /Hide Div

},

Function () {$ (this). attr ("style", "display:block;"); /Show Div

}

);

<div id= "Demo" ></div>

Note:

$ ("#demo"). Show () represents Display:block,
$ ("#demo"). Hide () indicates display:none;

The Display:none in 1 and 2 can be replaced by Visibility:hidden,display:block and replaced by visibility:visible. The difference between the two is that the former does not occupy space, and the latter will occupy space after hiding.

Cases:

[JavaScript]View PlainCopy
    1. <script type="Text/javascript" >
    2. function ShowDiv1 () {
    3. //$ ("#test1"). attr ("style", "Display:block");
    4. //$ ("#test1"). Show ();
    5. $ ("#test1"). CSS ("display","block");
    6. }
    7. function ShowDiv2 () {
    8. //$ ("#test2"). attr ("style", "visibility:visible");
    9. $ ("#test2"). CSS ("visibility","visible");
    10. }
    11. function HiddenDiv1 () {
    12. $ ("#test1"). Hide ();
    13. }
    14. function HiddenDiv2 () {
    15. $ ("#test2"). attr ("style","Visibility:hidden");
    16. }
    17. </script>
    18. <body>
    19. <div id="test1" style="Display:none" >aaaaaa</div> <%--hidden div--%>
    20. <div id="test2" style="Visibility:hidden" >bbbbb</div> <%--hidden div--%>
    21. <button onclick="ShowDiv1 ()" > Show 1</button>
    22. <button onclick="ShowDiv2 ()" > Show 2</button>
    23. <button onclick="HiddenDiv1 ()" > Hidden 1</button>
    24. <button onclick="HiddenDiv2 ()" > Hidden 2</button>
    25. </body>

jquery shows several ways to hide a div

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.