dom_dom0400_002

既存の親要素の末尾に子要素を追加する古典的APIはparent.( A )Child(child)で、同様の目的で複数ノードや文字列も一度に追加できる近年のAPIはparent.( B )(…)である。

解答

A: append
B: append

解説

appendChildは単一ノードのみ。appendは文字列や複数ノードを受け取れる上位互換的API(戻り値はundefined)。

使用例(サンプルコード)

[ html ]

<div id="root"></div>
<script>
  const root = document.getElementById('root');
  const a = document.createElement('span');
  a.textContent = 'A';
  root.appendChild(a);

  const b = document.createElement('span');
  b.textContent = 'B';
  root.append(' + ', b); // 文字列とノードを同時に
</script>