js

[javaScript] .remove() / .removeChild()

natrue 2021. 1. 12. 15:39
728x90

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ

HTML ์š”์†Œ๋ฅผ ๋™์ ์œผ๋กœ ์‚ญ์ œํ•˜๊ธฐ

 

 

1. remove : ์š”์†Œ๋ฅผ ์‚ญ์ œ 

// remove
function remove1Function(){
    var veBtnRmv = document.getElementById("veBtnId");
    veBtnRmv.remove();
    }
   

 

 

2. removeChild : ๋ถ€๋ชจ์—๊ฒŒ์„œ ์ž์‹ ๊ฐ์ฒด๋ฅผ ๋–ผ์–ด๋‚ด๊ธฐ (=์‚ญ์ œ)

// removeChild
function remove2Function(){
    var veList = document.getElementById("veList");
    veList.removeChild(veList.childNodes[0]);
    }

 

 


Test


 

 

 

 

childNodes  ์ž์‹ ๋…ธ๋“œ์— ์ ‘๊ทผ NodeList ๋ฐ˜ํ™˜
children  ์ž์‹ ์š”์†Œ์— ์ ‘๊ทผ HTMLCollection ๋ฐ˜ํ™˜