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