Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treewalker iteration

Tags:

javascript

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?

like image 515
Darlyn Avatar asked Jun 07 '15 17:06

Darlyn


1 Answers

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);
like image 129
Bergi Avatar answered Sep 22 '22 22:09

Bergi