괴발개발/Javascript+JQuery

JQ_요소제거(remove, empty)

moonday 2021. 6. 23. 20:50

remove -박스자체가 전부사라짐(본인포함), empty- 안에있는박스만사라짐

- 요소 제거(본인포함인지 아닌지가 다름)       
 - remove : 요소를 포함하여 자손요소들까지 같이 제거
 - empty : 요소를 제외하고 자손 요소들을 제거  
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>요소제거예제</title>
  <script src="../JS/jquery.min.js"></script>
  <script>
    $(function(){
      $('#btn1').click(function(){
        $('.box1').remove();
      })
      $('#btn2').click(function(){
        $('.box2').empty();
      });
    });
  </script>
  <style>
    .box1, .box2{
      height:50px; background: burlywood; margin-top: 20px;
    }
    .box1{
      background: rosybrown;
    }
  </style>
</head>
<body>
  <button id="btn1">remove</button>
  <button id="btn2">empty</button>
  <div class="box1">
    <input type="text">
  </div>
  <div class="box2">
    <input type="text">
  </div>
</body>
</html>