Document.createNodeIterator()
const nodeIterator = document.createNodeIterator(root[, whatToShow[, filter]]); 语法: 遍历起始处的根节点
显示所有节点: NodeFilter.SHOW_ALL
显示文本节点: NodeFilter.SHOW_TEXT
实例:
const nodeIterator = document.createNodeIterator( document.body, NodeFilter.SHOW_ELEMENT, { acceptNode(node) { return node.nodeName.toLowerCase() === 'p' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT; } } ); const pars = []; let currentNode; while (currentNode = nodeIterator.nextNode()) { pars.push(currentNode); }