ChildNode.remove()
메소드는 이를 포함하는 트리로부터 객체를 제거합니다.
문법
node.remove();
예제
remove() 사용하기
<div id="div-01">div-01 입니다</div>
<div id="div-02">div-02 입니다</div>
<div id="div-03">div-03 입니다</div>
var el = document.getElementById('div-02');
el.remove(); // id가 'div-02' 인 div를 제거합니다
ChildNode.remove() 는 스코프 지정 불가
remove()
메소드는 with
구문으로 스코프를 지정할 수 없습니다. 자세한 내용은 Symbol.unscopables
을 확인하세요.
with(node) {
remove();
}
// ReferenceError: remove is not defined
폴리필
인터넷 익스플로러 9 이상에서는 다음 코드를 사용해 remove() 메소드
를 폴리필링할 수 있습니다.
// from:https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('remove')) {
return;
}
Object.defineProperty(item, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function remove() {
if (this.parentNode !== null)
this.parentNode.removeChild(this);
}
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
명세
명세 | 상태 | 코멘트 |
---|---|---|
DOM The definition of 'ChildNode.remove' in that specification. |
Living Standard | 초기 정의. |
브라우저 호환성
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
함께 보기
ChildNode
순수 인터페이스.