f_
frhyme.code
•web•⏱ 약 2분 소요

JQuery - Remove Element

  • jQuery를 사용하여 element를 추가하고 지우는 방법을 정리합니다.
<html>
    <head>
        <!--
        CDN을 통해서 jquery를 가져옵니다.
        -->
        <script 
            src="https://code.jquery.com/jquery-3.5.1.js" 
            integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" 
            crossorigin="anonymous">
        </script>
        <style>
            .cls1 {
                width: 300px;
                height: 30px;
                background-color: blue;
                color:white;
            }
        </style>
        <script>
            $(document).ready(function() {
                $("#btn1").click(function() {
                    // #div1 내에 새로운 p 요소를 추가해줍니다.
                    $("#div1").append("<p>new paragraph</p>")
                })
                $("#btn2").click(function() {
                    // #div1 내의 모든 요소를 삭제해줍니다.
                    $("#div1").empty()
                })
                $("#btn3").click(function() {
                    // #div1의 자식 요소들 중에 첫번째 p 요소를 삭제합니다.
                    $("#div1").children().first("p").remove()
                })
            });
        </script>
    </head>
    <body>
        <button id="btn1">Append new p in div1</button>
        <button id="btn2">Empty div1</button>
        <button id="btn3">Remove First p</button>
        <div id="div1">This is div1</div> 
    </body>
</html>
GitHub에서 이 글의 마크다운 원본 보기 & 수정 제안src/content/posts/2021-02-10-jquery05_remove_element

💡RELATED ARTICLES · 함께 읽으면 좋은 연관 포스트

글 주소가 클립보드에 복사되었습니다.