문제

I have three inquiries or questions about nodeList type in javascript.

  1. What is nodeList type ?
  2. Is it like array, when we dealing with it ?
  3. How to know if this is nodeList type ?

For example

document.querySelector() // did not return nodeList
document.querySelectorAll() // return nodeList
//etc....
도움이 되었습니까?

해결책

1) A nodeList is an object – an indexed (ordered) list of nodes. It has a length property that returns the number of nodes in the list, and a method of item() that returns the node at a specified index.

There's also an HTMLCollection

2) It is kind of like an array. You can use a for loop, but you can't use array methods on them or HTMLCollections because they do not inherit from the array object.

3) That's one's tricky. You essentially use slice, but it fails in IE9… Here's an article that explains it in detail: duruk.net/nodelists-and-arrays-in-javascript/

One more thing to consider: nodeLists and HTMLCollections are live collections, meaning that they reflect updates to them in the DOM in real time, with the exception of when it is returned by .querySelectorAll.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top