상세 컨텐츠

본문 제목

[자바스크립트] body에 appendchild하기

카테고리 없음

by esoesmio 2023. 5. 23. 17:26

본문

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div id="parent-id">
    <p>hello word1</p>
    <p id="test1">hello word2</p>
    <p>hello word3</p>
    <p>hello word4</p>
</div>
<script>
let element = document.createElement('div');
element.id = 'test';
document.body.appendChild(element);
document.getElementById('test').textContent = 'hahasibar';


const $ul = document.querySelector('section > ul')
['상추','가지','브로콜리'].forEach(itm=>{
   $el = document.createElement('li');
   $el.textContent = itm;
   $ul.appendChild($el);


});

</script>
</body>
</html>

댓글 영역