admin管理员组

文章数量:1195318

I wanted to traverse node by node on a web page by maintaining sequence.

e.g. Below is basic DOM :

<BODY>
     <DIV id ='1'> Test 1 </DIV>
     <DIV id='2'> Details about <SPAN id='3'> Execution </SPAN> </DIV>   
</BODY>

As per above example I want traverse each node by node i.e.

1st Traversal : <BODY>

2nd Traversal : <DIV id ='1'>

3rd Traversal : <DIV id='2'>

4rd Traversal : <SPAN id='3'>

My motive is to loop over all the nodes available on current page and visit each node one by one saying simply nextnode(), while traversing not looking in to parent and child relations. Exepcted is, it should visit each node by following sequence.

So my statement will be like this :

startnode //consider this is start node

While ( startnode!=null ) {

  // will process on startnode

   startnode= startnode->nextnode();
  // something like that to visit each node
}

Is any one knows about this, how to achieve this using jquery(preferably) or javascript, please share their references.

Thanks

-Pravin

I wanted to traverse node by node on a web page by maintaining sequence.

e.g. Below is basic DOM :

<BODY>
     <DIV id ='1'> Test 1 </DIV>
     <DIV id='2'> Details about <SPAN id='3'> Execution </SPAN> </DIV>   
</BODY>

As per above example I want traverse each node by node i.e.

1st Traversal : <BODY>

2nd Traversal : <DIV id ='1'>

3rd Traversal : <DIV id='2'>

4rd Traversal : <SPAN id='3'>

My motive is to loop over all the nodes available on current page and visit each node one by one saying simply nextnode(), while traversing not looking in to parent and child relations. Exepcted is, it should visit each node by following sequence.

So my statement will be like this :

startnode //consider this is start node

While ( startnode!=null ) {

  // will process on startnode

   startnode= startnode->nextnode();
  // something like that to visit each node
}

Is any one knows about this, how to achieve this using jquery(preferably) or javascript, please share their references.

Thanks

-Pravin

Share Improve this question asked Sep 9, 2010 at 13:56 pravinpravin 2,1558 gold badges37 silver badges50 bronze badges 6
  • 1 @pravin Are you looking for $('*') ? – user372551 Commented Sep 9, 2010 at 13:59
  • @Avinash : M sorry, can you bit explore this....do u mean to say is all nodes on page... – pravin Commented Sep 9, 2010 at 14:01
  • 1 In $('*'), " * " selector is used to select Every node on your page. – user372551 Commented Sep 9, 2010 at 14:04
  • I think it wont select text Node...like in above example innerHTML of <div id='1'> i.e. Test 1. Here it will jump prper nodes like <body>,<div>,<div>,<span>. How do i achieve if i want : <body> , <div>, Test 1, <div>,Details about, <SPAN>, Execution is this possible ? – pravin Commented Sep 9, 2010 at 14:08
  • @pravin - You can get the content of those nodes while you're iterating over them, but your example does not have the text node, you should update the question for exactly what you're after. – Nick Craver Commented Sep 9, 2010 at 14:11
 |  Show 1 more comment

3 Answers 3

Reset to default 14

There's always the standard Crockford walk the dom method.

Example: http://jsfiddle.net/FJeaY/

var walk_the_DOM = function walk(node, func) {
    func(node);
    node = node.firstChild;
    while (node) {
        walk(node, func);
        node = node.nextSibling;
    }
};

walk_the_DOM(document.body, function(node) {
    if(node.nodeType == 1)
        alert(node.id);  // alert if we have a type 1 node
})​;​

Specific walk_the_DOM code example copied from here: http://snipplr.com/view/19815/walking-the-dom/

EDIT: Text nodes have nodeType = 3, so you can add that to your if() statement if those are desired as well.

walk_the_DOM(document.body, function(node) {
    if(node.nodeType == 1 || node.nodeType == 3)
        alert(node.id);  // ID will be undefined if it is a text node
})​;​

You can loop through with a body and all selector, like this:

$("body").find("*").andSelf().each(function() {
    alert(this.nodeName); //alerts body, div, div, span
});​

Note: andSelf has been deprecated and is now an alias for addBack(), which should be used with jQuery 1.8 and later

You can give it a try here, the body portion is so you don't get the <head> and it's contents, etc.

Simple.

function walk(node, fn) {
    if (node) do {
        fn(node);
        if (node.nodeType === 1) walk(node.firstChild, fn);
    } while (node = node.nextSibling);
}

Usage:

walk(document.body, function(){
    // do something with `this`
    // e.g.
    alert(this.id);
});

And to avoid non-element nodes, this will work:

function walk(node, fn) {
    if (node) do {
        if (node.nodeType === 1) {
            fn(node);
            walk(node.firstChild, fn);
        }
    } while (node = node.nextSibling);
}

本文标签: javascriptDOM traversal one by one node using jqueryStack Overflow