admin管理员组

文章数量:1335106

OK, I have a little function walking the tree like this:

function walkTree(node, func, args) {
    func(node, args);
    node = node.firstChild;
    while (node) {
        walkTree(node, func, args);
        node = node.nextSibling;
    }
}

And another function that would pick up only the text nodes like so:

function selectTextNodes(node, nodes) {
    if (node instanceof Text) {
        nodes.push(node);
    }
}

And finally, using both:

texts = [];
walkTree(body, selectTextNodes, texts);

However, it doesn't fill the list at all!

If I were to modify the test as to use Node.nodeType it would work:

function selectTextNodes(node, nodes) {
    if (node.nodeType == Node.TEXT_NODE) {
        nodes.push(node);
    }
}

On the other hand, in the console it works both ways:

t = window.document.createTextNode("test");
r = (t.nodeType == Node.TEXT_NODE) && (t instanceof Text);

That is, r is true.

Note that, all functions are nested inside another function that receives the body variable. In my case, this is the contentDocument.body of an iframe. There is no x-domain restriction being applied.

Any idea what's going on?

OK, I have a little function walking the tree like this:

function walkTree(node, func, args) {
    func(node, args);
    node = node.firstChild;
    while (node) {
        walkTree(node, func, args);
        node = node.nextSibling;
    }
}

And another function that would pick up only the text nodes like so:

function selectTextNodes(node, nodes) {
    if (node instanceof Text) {
        nodes.push(node);
    }
}

And finally, using both:

texts = [];
walkTree(body, selectTextNodes, texts);

However, it doesn't fill the list at all!

If I were to modify the test as to use Node.nodeType it would work:

function selectTextNodes(node, nodes) {
    if (node.nodeType == Node.TEXT_NODE) {
        nodes.push(node);
    }
}

On the other hand, in the console it works both ways:

t = window.document.createTextNode("test");
r = (t.nodeType == Node.TEXT_NODE) && (t instanceof Text);

That is, r is true.

Note that, all functions are nested inside another function that receives the body variable. In my case, this is the contentDocument.body of an iframe. There is no x-domain restriction being applied.

Any idea what's going on?

Share Improve this question edited Dec 15, 2012 at 19:01 Albus Dumbledore asked Dec 15, 2012 at 17:49 Albus DumbledoreAlbus Dumbledore 12.6k24 gold badges68 silver badges107 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

There are different Text interfaces in the different windows. So, if you have a DOM node from your iframe document, it is not an instanceof window.Text, but an instanceof iframe.contentWindow.Text. Afaik, also the availability of the Text interface as a Javascript object is non-standard.

That is why you just should check the nodeType of the elements. But notice that (older?) IE does not support the TEXT_NODE constant on Node, so you will either need to pare with 3 or assign that value as a polyfill to Node.TEXT_NODE.

Main document

<body>
  <iframe name="ifr" id="ifr" src='test1.html'></iframe>
  <button onclick="clkfn()">test</button>

  <script>

    function wrapper(body, iframeWindow) {
      function walkTree(node, func, args) {
        func(node, args);
        node = node.firstChild;
        while (node) {
            walkTree(node, func, args);
            node = node.nextSibling;
        }
      }

      function selectTextNodes(node, nodes) {
          if (node instanceof iframeWindow.Text) {
              nodes.push(node);
          }
      }    

      texts = [];
      walkTree(body, selectTextNodes, texts);

      for (var i = 0; i < texts.length; i++) {    
        console.log("text #" + i + texts[i].nodeValue);
      }
    }

    function clkfn() {
      var ifr = frames["ifr"];
      wrapper(ifr.document.body, ifr);
    }

  </script>


</body>

iFrame

<!doctype html>
<html>
    <head>
    </head>
    <body>
      how are you?
      I am fine. Thank you!
    </body>
</html>

console

When you click on the button, console prints:

   text #0  
        how are you?  
        I am fine. Thank you!

Only change made to code is passed the iframe body and window. Then referred the Text object from iFrame window.

本文标签: javascriptHaving trouble with DOM nodes and instanceofStack Overflow