大家好,我在学习js的基础知识,在学习遍历文档节点时,有一段代码,为什么不能正常运行呢,代码如下
<html>
<head>
<script>
// This function is passed a DOM Node object and checks to see if that node
// represents an HTML tag-i.e., if the node is an Element object. It
// recursively calls itself on each of the children of the node, testing
// them in the sameway. It returns the total number of Element objects
// it encounters. If you invoke this function by passing it the
// Document object, it traverses the entire DOM tree.
function countTags(n){
var numtags = 0;
if (n.nodeType == 1 /*Node.ELEMENT_NODE*/)
numtags++;
var children = n.childNodes;
for(var i=0; i<children.length;i++){
numtags+=countTags(childen[i]);
}
return numtags;
}
</script>
</head>
<!-- Here's an example of how the countTags() function might be used -->
<body onload="alert('This document has '+countTags(document)+'tags')">
This is a <i>sample</i> document.
</body>
</html>
<html>
<head>
<script>
// This function is passed a DOM Node object and checks to see if that node
// represents an HTML tag-i.e., if the node is an Element object. It
// recursively calls itself on each of the children of the node, testing
// them in the sameway. It returns the total number of Element objects
// it encounters. If you invoke this function by passing it the
// Document object, it traverses the entire DOM tree.
function countTags(n){
var numtags = 0;
if (n.nodeType == 1 /*Node.ELEMENT_NODE*/)
numtags++;
var children = n.childNodes;
for(var i=0; i<children.length;i++){
numtags+=countTags(childen[i]);
}
return numtags;
}
</script>
</head>
<!-- Here's an example of how the countTags() function might be used -->
<body onload="alert('This document has '+countTags(document)+'tags')">
This is a <i>sample</i> document.
</body>
</html>
解决方案 »
- jquery autocomplete中文检索问题
- 又到周末了!问个问题就下班。
- 关于内容分页的问题,各位大哥们帮帮我!
- SVG中如何引用其他文件中的标识符
- javascript到底全局变量作用域是什么,为什么在函数中改变了无效??在线等!~急,请知道的人进来回答下好吗?
- <%@LANGUAGE="JAVASCRIPT" CODEPAGE="936"%>如何做防SQL注入?
- 高手给看看,为什么会出现这个问题:此计算机上的安全设置禁止访问其他域的数据源。
- 在JSP页面里用JS来转向网页
- 怎么让onclik只在DIV A上面触发?
- jquery validate 前端验证,有时生效,有时不生效,求助!
- 求一个简单的正则表达式
- a标签里的href="#name=langl"是给url地址传的参数吧???
function countTags(n){
var numtags = 0;
if (n.nodeType == 1 /*Node.ELEMENT_NODE*/)
numtags++;
var children = n.childNodes;
for(var i=0; i <children.length;i++){
numtags+=countTags(children[i]); //children 打错了你
}
return numtags;
}
</script>