append와 appendChild에 대해 알아보기

Minju Kang·2023년 8월 27일
0

append와 appendChild에 대해 알아보자


append와 appendChild의 공통점

  • 부모 노드에 자식 노드를 추가하는 메소드
  • 메서드를 호출한 노드의 마지막 자식 노드로 추가

append와 appendChild의 차이점

appendChild

  • appendChild는 노드 객체만 추가할 수 있음
  • 문자열 추가하면 에러 발생
  • return값을 반환해줌
const myLi = document.createElement('li');
const myP = document.createElement('p');

myP.appendChild('text'); // 에러 발생

myP.innerText = 'text';
myLi.appendChild(myP);  //<p>text</p>

append

  • append는 노드 객체 뿐 아니라 문자열도 추가할 수 있음
  • 한번에 2개 이상의 자식 노드를 추가할 수 있음
  • return값을 반환하지 않음
const myLi = document.createElement('li');
const myP = document.createElement('p');
myP.append('text1');
myLi.append(myP,'text2');
console.log(myLi)
//  <li>
//		<p>text1</p>
//		"text2"
//  </li>
profile
나의 기억저장소

0개의 댓글