라이브러리

[JAVASCRIPT] element.removeChild(child) - 자식 요소 제거




자바스크립트에서 element.removeChild(child) 메서드


자바스크립트에서 `element.removeChild(child)` 메서드는 HTML 요소의 자식 노드를 제거하는 데 사용됩니다. 이 메서드는 `child` 노드가 `element` 요소의 자식 노드인 경우에만 작동합니다.

# 메서드의 매개 변수


* `child`: 제거할 자식 노드

# 메서드의 반환 값


* 제거된 자식 노드

# 예제


#hostingforum.kr
javascript

// HTML 요소 생성

const parent = document.createElement('div');

const child1 = document.createElement('p');

const child2 = document.createElement('span');



// 자식 노드 추가

parent.appendChild(child1);

parent.appendChild(child2);



// 자식 노드 제거

const removedChild = parent.removeChild(child1);



console.log(removedChild); // 제거된 자식 노드

console.log(parent.children); // 제거된 자식 노드가 제거된 후의 자식 노드 목록



# 예제 설명


이 예제에서는 `div` 요소를 생성하고 `p` 요소와 `span` 요소를 자식 노드로 추가합니다. 그런 다음 `removeChild` 메서드를 사용하여 `p` 요소를 제거합니다. 제거된 요소는 `removedChild` 변수에 저장되고, 제거된 후의 자식 노드 목록은 `parent.children` 속성에 저장됩니다.

# 주의 사항


* `removeChild` 메서드는 `child` 노드가 `element` 요소의 자식 노드인 경우에만 작동합니다.
* `removeChild` 메서드는 `child` 노드를 제거한 후에 `element` 요소의 자식 노드 목록이 업데이트됩니다.
* `removeChild` 메서드는 `child` 노드를 제거한 후에 `child` 노드의 참조가 유효하지 않습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 1 페이지

검색

게시물 검색