简体   繁体   中英

why the label "p" don't slide down when i click the button "hide" or "show"

When i click the button hide or show,the label "p" don't slide down.But when the function is fadeToggle it can run. How can i let the label "p" slide down when i click hide or show?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery</title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        $(document).ready(
            function () {
                $("button").click
                (   function ()
                    {
                       // $("p").fadeToggle("slow");
                       // $("p").fadeTo("slow");
                        $("p").slideDown("slow");

                    }
                );
            }
        );

    </script>

</head>
<body>
    <p id="p1">hello show and hide</p>
    <button type="button">hide</button>
    <button type="button">show</button>
</body>



</html>

Your #p1 should be hidden before it can slide down. Please check my code below

 $(document).ready( function() { $("button").click(function() { // $("p").fadeToggle("slow"); // $("p").fadeTo("slow"); $("p").slideDown("slow"); }); } );
 #p1 { display: none }
 <:DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jquery</title> <script src="http.//code.jquery.com/jquery-latest.js"></script> </head> <body> <p id="p1">hello show and hide</p> <button type="button">hide</button> <button type="button">show</button> </body> </html>

You can make your p tag hidden onload and then when any button ie: hide/show is clicked check if it has a particular class depending on this slideDown or slideUp your p tag.

Demo Code :

 $("button").click(function() { //check if the button has show class.. $(this).hasClass("show")? $("p").slideDown("slow"): $("p").slideUp("slow"); });
 p#p1 { display: none }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="p1">hello show and hide</p> <!--added hide/show class--> <button type="button" class='hide'>hide</button> <button type="button" class='show'>show</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM