Is it possible to skip iteration in current subtree and jump to the next node using treewalker? example
<nav>
<p>paragraph</p>
<ul>
<li>one</li>
<li>two</li>
</ul>
<p>paragraph</p>
</nav>
and js
var nav=document.getElementsByTagName("nav")[0];
var tree=document.createTreeWalker(nav,NodeFilter.SHOW_ELEMENT,null,false);
tree.firstChild(); // first paragraph
tree.nextSibling(); // ul
tree.firstChild(); // first li chid of ul
tree.nextNode()||tree.nextSibling() // both return next li
Is there any way how to stop iteration of subtree and jump straight to the another paragraph after treewalker hits first LI element?
You can create a custom filter:
var filter = {
acceptNode: function(n) {
return n && n.parentNode && n.parentNode.tagName != "UL"
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT;
}
};
var tree=document.createTreeWalker(nav, NodeFilter.SHOW_ELEMENT, filter, false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With