function countChars(elm) {
if (elm.nodeType == 3) { // TEXT_NODE
return elm.nodeValue.length;
}
var count = 0;
for (var i = 0, child; child = elm.childNodes[i]; i++) {
count += countChars(child);
}
return count;
}
我想通过这个函数的字符串像countChars("hello")
;但那不起作用。什么是我可以传递的元素的例子?这个函数需要什么样的参数?
1为分销商从特定的*元素节点*中抛出一个通用的* DOM节点*。 – 2012-01-31 20:29:57
+1在此1分钟内最准确和最具说明性。 :-) – JimmiTh 2012-01-31 20:33:32