国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > HTML5 > 正文

HTML5 HTMLCollection和NodeList的區(qū)別詳解

2024-09-05 07:23:26
字體:
來源:轉載
供稿:網(wǎng)友

本文主要介紹了HTML5 HTMLCollection和NodeList的區(qū)別詳解,分享給大家,具體如下:

獲取

HTMLCollection 對象

getElementsByTagName() 方法返HTMLCollection對象。
HTMLCollection 對象類似包含 HTML 元素的一個數(shù)組。

注意:

  • HTMLCollection 不是一個數(shù)組!
  • HTMLCollection 看起來可能是一個數(shù)組,但其實不是。
  • 你可以像數(shù)組一樣,使用索引來獲取元素。
  • HTMLCollection 無法使用數(shù)組的方法: valueOf(), pop(), push(), 或 join()。

NodeList 對象

大部分瀏覽器的querySelectorAll()返回 NodeList 對象。

注意

  • 節(jié)點列表不是一個數(shù)組!
  • 節(jié)點列表看起來可能是一個數(shù)組,但其實不是。
  • 你可以像數(shù)組一樣,使用索引來獲取元素。
  • 節(jié)點列表無法使用數(shù)組的方法: valueOf(), pop(), push(), 或 join() 。

HTMLCollection 與 NodeList 的區(qū)別

  1. HTMLCollection是 HTML 元素的集合。(僅包含元素)
  2. NodeList 是一個文檔節(jié)點的集合。
  3. NodeList 與 HTMLCollection 有很多類似的地方。
  4. NodeList 與 HTMLCollection 都與數(shù)組對象有點類似,可以使用索引 (0, 1, 2, 3, 4, ...) 來獲取元素。
  5. NodeList 與 HTMLCollection 都有 length 屬性。
  6. HTMLCollection 元素可以通過 name,id 或索引來獲取。
  7. NodeList 只能通過索引來獲取。
  8. 只有 NodeList 對象有包含屬性節(jié)點和文本節(jié)點。

代碼

<!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的區(qū)別詳解的文章就介紹到這了,更多相關HTML5 HTMLCollection NodeList內(nèi)容請搜索武林網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持武林網(wǎng)!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 张北县| 株洲县| 通州市| 五大连池市| 慈利县| 古浪县| 杂多县| 鄢陵县| 宁波市| 汉源县| 腾冲县| 社会| 布尔津县| 武鸣县| 攀枝花市| 玉屏| 三河市| 饶河县| 广宗县| 井研县| 奉贤区| 达尔| 张掖市| 延寿县| 桐梓县| 金寨县| 岐山县| 泰和县| 柳州市| 千阳县| 华阴市| 新密市| 乌鲁木齐县| 威宁| 陆良县| 万源市| 游戏| 通州区| 内乡县| 商水县| 诸城市|