最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

textnode - counting text node recursively using javascript - Stack Overflow

programmeradmin1浏览0评论

Let say I have a mark up like this

<html id="test">
<body>
Some text node.
<div class="cool"><span class="try">This is another text node.</span></div>
Yet another test node.
</body>
</html>

my js code

function countText(node){
 var counter = 0;
 if(node.nodeType === 3){
     counter+=node.nodeValue.length;
     countText(node);
 }
 else{}
}

Now if I want to count the text nodes

console.log("count text : " + countText(document.getElementById("test"));

this should return me the count but its not working and moreover what should I put in else condition. I never used nodeType so kind of having problem using it . Any help will be appreciated.

Let say I have a mark up like this

<html id="test">
<body>
Some text node.
<div class="cool"><span class="try">This is another text node.</span></div>
Yet another test node.
</body>
</html>

my js code

function countText(node){
 var counter = 0;
 if(node.nodeType === 3){
     counter+=node.nodeValue.length;
     countText(node);
 }
 else{}
}

Now if I want to count the text nodes

console.log("count text : " + countText(document.getElementById("test"));

this should return me the count but its not working and moreover what should I put in else condition. I never used nodeType so kind of having problem using it . Any help will be appreciated.

Share Improve this question edited Apr 14, 2011 at 3:28 user429035 asked Apr 14, 2011 at 0:05 user429035user429035 4711 gold badge5 silver badges15 bronze badges 2
  • Note that given the same HTML, different browsers may create different numbers of text nodes. – RobG Commented Apr 14, 2011 at 2:37
  • @RobG can you explain it briefly about the behavior... – user429035 Commented Apr 14, 2011 at 3:21
Add a ment  | 

2 Answers 2

Reset to default 7

There are a couple of things wrong in your code:

  • Your HTML is malformed.
  • You are appending text to your counter instead of increasing it.
  • You never loop over the children of the a node, you always pass the same node to the recursive call.
  • You don't do anything if a node is not a text node.

This will work:

function countText(node){
    var counter = 0;
    if(node.nodeType === 3){
        counter++;
    }
    else if(node.nodeType === 1) { // if it is an element node, 
       var children = node.childNodes;    // examine the children
       for(var i = children.length; i--; ) {
          counter += countText(children[i]);
       }
    }
    return counter;  
}

alert(countText(document.body));

DEMO

Which number corresponds to which node type can be found here.


Update:

If you want to count the words, you have to split each text node into words first. In the following I assume that words are separated by white spaces:

if(node.nodeType === 3){
    counter = node.nodeValue.split(/\s+/g).length;
}

Update 2

I know you want to use a recursive function, but if you want to count the words only, then there is a much easier and more efficient way:

function countWords(node){
    // gets the text of the node and all its descendants
    var text = node.innerText || node.textContent
    return text.split(/\s+/g).length;
}

You want something like

function countTextNodes(node) {
    var n = 0;
    if(node.nodeType == 3)
        n = 1;
    for(var i = 0; i < node.childNodes.length; ++i)
        n += countTextNodes(node.childNodes[i]);
    return n;
}

This can be pressed into more pact code, but I went for legibility here.

Call this on the root in which you want to count text nodes. For example, to count text nodes throughout the entire document, you would want to call countTextNodes(document.getDocumentElement()).

发布评论

评论列表(0)

  1. 暂无评论