append() vs appendChild()

ROCKBELL·2022년 11월 10일
0

공통점

  • 노드 객체 추가 가능

append()

  • 문자열 추가 가능
  • return 값이 없음
  • 여려개 자식 요소 추가 가능
     const div = document.createElement('div');
     const span = document.createElement('span');
     const p = document.createElement('p');
     
     console.log(document.body.append(div,'hello', span, p)); // output:  undefiend (return 값은 없음) 

appendChild()

  • 문자열 추가 불가 (Uncaught TypeError 발생)

    const parent = document.createElement('div');
    parent.appendChild('텍스트'); / Uncaught TypeError 발생
  • return 값 존재 (Node Object)

    const parent = document.createElement('div');
    const child = document.createElement('span');
    
    console.log(document.body.appendChild(div)); // output : div
  • 단일 자식 요쇼 추가 가능

profile
luv it

0개의 댓글