(85)Wangdao.com第十八天_JavaScript NodeList 接口,HTMLCollection 接口

NodeList 接口        HTMLCollection 接口node

节点都是单个对象,有时须要一种数据结构,可以容纳多个节点数组

DOM 提供两种节点集合,用于容纳多个节点:NodeList 和 HTMLCollection数据结构

 

这两种集合都属于接口规范。app

许多 DOM 属性和方法,返回的结果是 NodeList 实例或 HTMLCollection 实例。函数

主要区别是this

NodeList 能够包含各类类型的节点spa

HTMLCollection 只能包含 HTML 元素节点。prototype

 

NodeList 接口code

    • NodeList 实例是一个相似数组的对象,能够使用 length 属性和 forEach 方法。可是,它不是数组,不能使用 pop() 或 shift() 之类数组特有的方法。
        • var children = document.body.childNodes;
          
          Array.isArray(children);    // false
          
          children.length;    // 34
          children.forEach(console.log);    // forEach() 遍历 NodeList 实例

          for (var i = 0; i < children.length; i++) { // for 遍历 NodeList 实例 var item = children[i]; }
        • 若是 NodeList 实例要使用数组方法,能够将其转为真正的数组orm

          • var children = document.body.childNodes;
            var nodeArr = Array.prototype.slice.call(children);
    • 它的成员是节点对象。
    • 经过如下方法能够获得 NodeList 实例
      • Node.childNodes
      • document.querySelectorAll()等节点搜索方法
    • 注意:
      • NodeList 实例多是动态集合,也多是静态集合。
        • 所谓动态集合就是一个活的集合DOM 删除或新增一个相关节点,都会马上反映在 NodeList 实例
      • 目前,只有 Node.childNodes 返回的是一个动态集合,其余的 NodeList 都是静态集合。
    • var children = document.body.childNodes;
      children.length;    // 18
      document.body.appendChild(document.createElement('p'));
      children.length;    // 19    // 文档增长一个子节点,NodeList 实例 的 属性就增长了1childrenlength

 

  • NodeList.prototype.length
    • 返回 NodeList 实例包含的节点数量。
    • 对于那些不存在的 HTML 标签,length 属性返回 0
      document.getElementsByTagName('xxx').length;    // 0

       

  • NodeList.prototype.forEach()
    • 用于遍历 NodeList 的全部成员。
    • 它接受一个回调函数做为参数,每一轮遍历就执行一次这个回调函数
    • 用法与数组实例的 forEach() 方法彻底一致
      • var children = document.body.childNodes;
        children.forEach(function f(item, i, list) {
            // ...
        }, this);    // forEach方法的第二个参数,用于绑定回调函数内部的this,该参数可省略
        // 回调函数f的三个参数依次是
            // 当前成员
            // 在类数组中的 index
            // 当前 NodeList 实例。

 

  • NodeList.prototype.item( posIndex )
    • 接受一个整数值做为参数,表示成员的位置,返回该位置上的成员
    • 全部相似数组的对象,均可以使用方括号运算符取出成员。通常状况下,都是使用方括号运算符,而不使用item方法。
      • document.body.childNodes.item(0);    // item(0)返回第一个成员
        document.body.childNodes[0]; // 最为经常使用 

         

    • 若是参数值大于实际长度,或者索引不合法(好比负数),item方法返回null。
    • 若是省略参数,item 方法会报错

 

  • NodeList.prototype.keys(),NodeList.prototype.values(),NodeList.prototype.entries()
    • 这三个方法都返回一个 ES6 的遍历器对象
    • 能够经过for...of循环遍历获取每个成员的信息
    • 区别在于
      • keys()返回键名的遍历器
      • values()返回键值的遍历器
      • entries()返回的遍历器同时包含键名和键值的信息。
    • var children = document.body.childNodes;
      
      for (var key of children.keys()) {
          console.log(key);
      }
      // 0
      // 1
      // 2
      // ...
      
      for (var value of children.values()) {
          console.log(value);
      }
      // #text
      // <script>
      // ...
      
      for (var entry of children.entries()) {
          console.log(entry);
      }
      // Array [ 0, #text ]
      // Array [ 1, <script> ]
      // ...

 

HTMLCollection 接口 

  • 概述
    • HTMLCollection 是一个节点对象的集合,只能包含元素节点(element),不能包含其余类型的节点。
    • 它的返回值是一个相似数组的对象,可是与NodeList接口不一样,HTMLCollection没有forEach方法只能使用for循环遍历

 

    • 返回 HTMLCollection 实例的,主要是些 document 对象的集合属性
      • 好比 document.forms、document.images、document.links 等等
        • document.links instanceof HTMLCollection    // true
    • HTMLCollection 实例都是动态集合,节点的变化会实时反映在集合中

 

    • 若是元素节点有 id 或 name  属性,那么 HTMLCollection  实例上面,
      • 能够使用 id 属性或 name 属性引用该节点元素。
      • 若是没有对应的节点,则返回null
      • // HTML 代码以下
        // <img id="pic" src="http://example.com/foo.jpg">
        
        var pic = document.getElementById('pic');
        document.images.pic === pic    // true

         

  • HTMLCollection.prototype.length
    • 返回 HTMLCollection 实例包含的成员数量
      document.links.length    // 18

 

  • HTMLCollection.prototype.item()
    • item() 方法接受一个整数值做为参数,表示成员的位置,返回该位置上的成员。
    • 因为方括号运算符也具备一样做用,并且使用更方便,因此通常状况下,老是使用方括号运算符
    • 若是参数值超出成员数量或者不合法(好比小于0),那么 item() 方法返回 null
      • var c = document.images;
        var img0 = c.item(0);
        
        var img0 = c[0];    // 更经常使用 [ ]

 

  • HTMLCollection.prototype.namedItem()
    • 参数是一个字符串,表示 id 属性或 name 属性的值,返回对应的元素节点。
    • 若是没有对应的节点,则返回null
      • // HTML 代码以下
        // <img id="pic" src="http://example.com/foo.jpg">
        
        var pic = document.getElementById('pic');
        document.images.namedItem('pic') === pic    // true 
相关文章
相关标签/搜索