Сообщение от Malleys
|
Обход всех текстовых узлов в DOM
|
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
</head>
<body>
<div>12345</div>
<script>
var Y=f=>(g=>x=>f(g(g))(x))(g=>x=>f(g(g))(x));
var processTextNodes = visit => Y(order => node => {
if(node != null) node.childNodes.forEach(childNode =>
childNode.nodeType === Node.TEXT_NODE ? visit(childNode) : order(childNode)
)
});
var logTextNotes = processTextNodes(console.log);
logTextNotes(document.body);
/* https://developer.mozilla.org/ru/docs/Web/API/document/createTreeWalker */
function textNodesLog(el){
const tree = document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while(tree.nextNode()) console.log(tree.currentNode);
}
textNodesLog(document.body)
</script>
</body>
</html>