Remove all child elements of a DOM node in JavaScript

Posted by RAY.D
2016. 3. 28. 08:07 Web/javascript / jQuery
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Remove all child elements of a DOM node in JavaScript




Option 1 (much slower, see comments below):

var myNode = document.getElementById("foo");
myNode.innerHTML = '';

Option 2 (much faster):

var myNode = document.getElementById("foo");
while (myNode.firstChild) {
    myNode.removeChild(myNode.firstChild);
}