www.webdeveloper.com
Results 1 to 3 of 3

Thread: HElP!Traversing the nodes of a document,but null in the strings!

  1. #1
    Join Date
    Jul 2012
    Posts
    5

    Question HElP!Traversing the nodes of a document,but null in the strings!

    <html>
    <head>
    <title>createTextNode example</title>

    <script type="text/javascript">
    var i=0;
    function addTextNode()
    {
    var newtext = document.createTextNode(" Some text added dynamically. "+(++i));
    var para = document.getElementById("p1");
    para.appendChild(newtext);
    }
    function getText(n) {
    // Repeated string concatenation can be inefficient, so we collect
    // the value of all text nodes into an array, and then concatenate
    // the elements of that array all at once.
    var strings = [];
    getStrings(n, strings);
    //return strings.join("");

    // This recursive function finds all text nodes and appends
    // their text to an array.
    function getStrings(n, strings) {
    if (n.nodeType == 3 /* Node.TEXT_NODE */)
    strings.push(n.data);
    else if (n.nodeType == 1 /* Node.ELEMENT_NODE */) {
    // Note iteration with firstChild/nextSibling
    for(var m = n.firstChild; m != null; m = m.nextSibling) {
    getStrings(m, strings);
    }
    }
    }
    alert(strings);
    }
    </script>
    </head>

    <body >
    <div style="border: 1px solid red">
    <p id="p1">First line of paragraph.<br /></p>
    </div><br />

    <button onclick="addTextNode();">add another textNode.</button>
    <button onclick="getText(document);">GetText</button>
    </body>
    </html>
    ------------------------------------------------
    getText(document)work,but the strings can't get anything.The alert(strings) is null . why??

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,689
    Code:
    <html>
    <head>
    <title>createTextNode example</title>
    
    <script type="text/javascript">
    var i=0;
    function addTextNode()
    {
    var newtext = document.createTextNode(" Some text added dynamically. "+(++i));
    var para = document.getElementById("p1");
    para.appendChild(newtext);
    }
    
    function getText(n) {
    // Repeated string concatenation can be inefficient, so we collect
    // the value of all text nodes into an array, and then concatenate
    // the elements of that array all at once.
    var strings = [];
    getStrings(n, strings);
    alert(strings);
    //return strings.join("");
    }
    
    // This recursive function finds all text nodes and appends
    // their text to an array.
    function getStrings(node,ary){
     var num=node.childNodes.length,z0=0,txt,fc,z0a,sp;
     for (;z0<num;z0++) {
      fc=node.childNodes[z0];
      if (fc.nodeType==3&&fc.data&&fc.data!=''){
        ary.push(fc.data);
      }
      else {
       getStrings(fc,ary);
      }
     }
    }
    
    
    </script>
    </head>
    
    <body >
    <div style="border: 1px solid red">
    <p id="p1">First line of paragraph.<br /></p>
    </div><br />
    
    <button onclick="addTextNode();">add another textNode.</button>
    <button onclick="getText(document);">GetText</button>
    </body>
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Jul 2012
    Posts
    5

    Thanks,vwphillips!you's code is very usful.

    Thanks,vwphillips!you's code is very usful.
    I made a mistake in my codes,and someone told me a very simple answer
    <button onclick="getText(document);">GetText</button> is not right.
    <button onclick="getText(document.body);">GetText</button> work !

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles