jQuery 수업

jQuery-5(이벤트 toggle)

줄라이퍼스트 2020. 2. 19. 11:11

- jQuery-4에서는 hide(), show()를 썻는데 이것보다는 toggle()를 사용하는 것을 추천합니다.

toggle()는 hide(), show()를 번갈아 가면서 실행을 합니다.

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
             
            $("#hide").click(function(){
                // hide(speed, callback)
                $("p").hide(1000);
            });

            
            $("#show").click(function(){
                // show(speed, callback)
                $("p").show(1000);
            });

            $("#toggle").click(function(){
                // hide와 show를 번갈아 실행
                $("h3").toggle();
            });

        });    

    </script>
</head>
<body>

    <p>If you click on the "Hide" button, I will disappear.</p>

    <button id="hide">Hide</button>
    <button id="show">Show</button>
    <button id="toggle">Toggle</button>

    <h3>This is h3</h3>
    <h3>This is another h3</h3>
    <h3>This is 3rd h3</h3>
</body>
</html>

 

※ 실행화면