수

.empty ()

· 2017-09-05 (화) 23:57:31 · 2107

.empty ()


설명 : 일치하는 요소 집합의 모든 자식 노드를 DOM에서 제거합니다.


이 메소드는 자식 (및 기타 자손) 요소뿐만 아니라 일치하는 요소 세트 내의 텍스트도 제거합니다. 이것은 DOM 사양에 따라 요소 내의 텍스트 문자열이 해당 요소의 자식 노드로 간주되기 때문입니다. 다음 HTML을 고려하십시오.


div class="container">

  <div class="hello">Hello</div>

  <div class="goodbye">Goodbye</div>

</div>

제거 할 요소는 모두 타겟팅 할 수 있습니다.


$( ".hello" ).empty();

이렇게하면 Hello텍스트가 삭제 된 DOM 구조가 생성 됩니다.


div class="container">

  <div class="hello"></div>

  <div class="goodbye">Goodbye</div>

</div>

내부에 중첩 된 요소 <div class="hello">가 여러 개 있으면 제거됩니다.


메모리 누수를 피하기 위해 jQuery는 요소 자체를 제거하기 전에 자식 요소에서 데이터 및 이벤트 핸들러와 같은 다른 구조를 제거합니다.


데이터 또는 이벤트 핸들러를 파괴하지 않고 요소를 제거하려는 경우 (나중에 다시 추가 할 수 있도록) .detach()대신 대신 사용하십시오.


예:

모든 단락에서 모든 하위 노드 (텍스트 노드 포함)를 제거합니다.


<!doctype html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>empty demo</title>

  <style>

  p {

    background: yellow;

  }

  </style>

  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

</head>

<body>

 

<p>

  Hello, <span>Person</span> <em>and person</em>.

</p>

 

<button>Call empty() on above paragraph</button>

 

<script>

$( "button" ).click(function() {

  $( "p" ).empty();

});

</script>

 

</body>

</html>

댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,404건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
jQuery
[jQuery]
17-09-10 조회 1,971
jQuery
[jQuery]
17-09-09 조회 1,896
jQuery 17-09-09 조회 2,746
jQuery 17-09-09 조회 4,120
PHP 17-09-08 조회 6,181
기타 17-09-08 조회 2,464
기타 17-09-08 조회 2,279
기타 17-09-08 조회 2,592
jQuery 17-09-07 조회 2,188
jQuery 17-09-07 조회 1,900
jQuery 17-09-07 조회 2,679
jQuery 17-09-06 조회 2,373
jQuery
[jQuery]
17-09-06 조회 2,148
jQuery 17-09-06 조회 2,433
jQuery
[jQuery]
17-09-05 조회 1,817
jQuery
[jQuery]
17-09-05 조회 2,108
jQuery
[jQuery]
17-09-05 조회 1,804
jQuery
[jQuery]
17-09-02 조회 1,740
jQuery 17-09-02 조회 2,041
jQuery 17-09-02 조회 2,147
jQuery 17-09-01 조회 2,142
jQuery 17-09-01 조회 2,285
jQuery 17-09-01 조회 2,012
jQuery 17-08-31 조회 2,391
jQuery 17-08-31 조회 2,498
jQuery 17-08-31 조회 1,932
PHP 17-08-31 조회 3,339
jQuery 17-08-29 조회 1,920
jQuery 17-08-29 조회 2,101
jQuery 17-08-29 조회 2,202