zoukankan      html  css  js  c++  java
  • js node 节点 原生遍历 createNodeIterator

    1、createIterator

        msn:

        https://developer.mozilla.org/en-US/docs/Web/API/Document/createNodeIterator

      var filter = function(node){

    return node.tagName.toLowerCase() == 'p' ?
        NodeFilter.FILTER_ACCEPT:
        NodeFilter.FILTER_REJECT;
    }
    
    
    var iterator = document.createNodeIterator(root, NodeFilter.SHOW_ELEMENT, filter, false);
    
    var node = iterator.nextNode();
    
    while(node !== null){
        console.log( node.tagName ); 
    // node 的属性 与方法 详见 2 nodeFilter node = iterator.nextNode(); } ———————————————— 基于此,可以简单的做为 web node 节点的数据 采集

    1、nodeFilter

        msn:

        https://developer.mozilla.org/en-US/docs/Web/API/NodeFilter

       

    var nodeIterator = document.createNodeIterator(
      // Node to use as root
      document.getElementById('someId'),
    
      // Only consider nodes that are text nodes (nodeType 3)
      NodeFilter.SHOW_TEXT,
    
      // Object containing the function to use for the acceptNode method
      // of the NodeFilter
        { acceptNode: function(node) {
          // Logic to determine whether to accept, reject or skip node
          // In this case, only accept nodes that have content
          // other than whitespace
          if ( ! /^s*$/.test(node.data) ) {
            return NodeFilter.FILTER_ACCEPT;
          }
        }
      },
      false
    );
    
    // Show the content of every non-empty text node that is a child of root
    var node;
    
    while ((node = nodeIterator.nextNode())) {
      alert(node.data);
    }

     3、element

      msn: https://developer.mozilla.org/zh-CN/docs/Web/API/Element

    while(node !== null){
        console.log( node.tagName ); 
       //node.tagName  等于 element.tagName 
      // element 的属性 与方法 详见 3、
    
        node = iterator.nextNode();
    }

      

    sfd

  • 相关阅读:
    【poj1655】Balancing Act
    yargs.js用法
    8、typescript
    7、typescript
    6、typescript
    5、typescript
    4、typescript
    3、typescript
    2、typescript
    1、typescript
  • 原文地址:https://www.cnblogs.com/cbugs/p/11398353.html
Copyright © 2011-2022 走看看