dom_dom0300_008

document.querySelectorAll(“p”) が返すオブジェクトの型はどれか。

  1. Array
  2. HTMLCollection
  3. NodeList
  4. Object
正解
  1. NodeList
解説

querySelectorAll() は NodeList を返す。getElementsByClassName() などは HTMLCollection を返すが、両者は似ていても異なるオブジェクトである。

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

[ javascript ]

let paragraphs = document.querySelectorAll("p");
console.log(paragraphs instanceof NodeList); // true