더북(TheBook)

형식

<부모 태그>.appendChild(<자식 태그>) 
<부모 태그>.append(<자식 태그1>, <자식 태그2>, ...)

인수에는 document.createElement()document.createTextNode() 메서드로 만든 태그나 텍스트를 넣습니다. appendChild()에는 하나만 넣을 수 있고, append()를 사용하면 여러 개를 동시에 넣을 수 있습니다. 또한, append()로 텍스트를 추가할 때는 document.createTextNode() 대신 문자열을 바로 넣어도 됩니다.

코드를 다음과 같이 수정하면 body 태그에 버튼이 추가된 것을 확인할 수 있습니다.

const $button = document.createElement('button');
$button.classList.add('login');
$button.style.fontSize = '15px';
$button.textContent = '버튼';
document.body.append($button);

document.bodybody 태그에 접근할 수 있는 DOM 속성입니다. head 태그에 접근하고 싶다면 document.head 속성을 사용합니다.

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.