本文主要介紹了HTML5 HTMLCollection和NodeList的區別詳解,分享給大家,具體如下:
獲取
HTMLCollection 對象
getElementsByTagName() 方法返HTMLCollection對象。
HTMLCollection 對象類似包含 HTML 元素的一個數組。
注意:
NodeList 對象
大部分瀏覽器的querySelectorAll()返回 NodeList 對象。
注意
HTMLCollection 與 NodeList 的區別
代碼
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <P>1</P> <P id="p2">2</P> <P>3</P> <P>4</P> <P>5</P> <script> // getElementsByTagName() 方法返回 HTMLCollection 對象。 const myCollection = document.getElementsByTagName('p'); console.log(myCollection) // 大部分瀏覽器的 querySelectorAll() 返回 NodeList 對象。 const myNodeList = document.querySelectorAll("p"); console.log(myNodeList) console.log(myNodeList ===myCollection) //false console.log(myCollection.p2) // <P id="p2">2</P> console.log(myNodeList.p2) //undefine </script></body></html>
到此這篇關于HTML5 HTMLCollection和NodeList的區別詳解的文章就介紹到這了,更多相關HTML5 HTMLCollection NodeList內容請搜索武林網以前的文章或繼續瀏覽下面的相關文章,希望大家以后多多支持武林網!
新聞熱點
疑難解答