【发布时间】:2011-08-05 03:42:10
【问题描述】:
假设我有这样的标记
<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>
我的 js 代码
function countText(node){
var counter = 0;
if(node.nodeType === 3){
counter+=node.nodeValue.length;
countText(node);
}
else{}
}
现在如果我想计算文本节点
console.log("count text : " + countText(document.getElementById("test"));
这应该返回给我计数,但它不起作用,而且我应该在 else 条件下放置什么。 我从来没有使用过nodeType,所以在使用它时遇到了问题。任何帮助将不胜感激。
【问题讨论】:
-
请注意,给定相同的 HTML,不同的浏览器可能会创建不同数量的文本节点。
-
@RobG 你能简单解释一下这种行为吗...
标签: javascript textnode