为什么if (node.attributes != null)正确,而if(node.attributes.length!=0)却出现错误

bbrr 2003-03-21 10:22:27
请问
为什么下面的函数会出错,
function Traverse(node)
{
nodes+=1;
if(node.attributes.length!=0)
nodes+=node.attributes.length;
for(var i=0;i<node.childNodes.length;i++)
Traverse(node.childNodes(i));
}
而下面这个却成功
function Traverse(node)
{
nodes+=1;
if(node.attributes!=null)
nodes+=node.attributes.length;
for (var i=0;i<node.childNodes.length; i++)
Traverse(node.childNodes(i));
}
...全文
106 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbrr 2003-03-22
  • 打赏
  • 举报
回复
谢谢大侠指点。
我想说一声,这里的论坛真不错。
saucer 2003-03-21
  • 打赏
  • 举报
回复
because some node has no attributes? then when you call node.attributes.length, it will complain node.attributes is null, you cannot access a null object's length property, since there is none

note, text is also a node, but it doesn't have an attributes property, for example:
<xml id="xmldoc">
<root>hello</root>
</xml>
<script language="javascript">
function window.onload()
{
var doc = xmldoc.documentElement;
alert(doc.attributes.length);
alert(doc.childNodes.length);
alert(doc.childNodes[0].nodeName + " has attributes? " + doc.childNodes[0].attributes)
}
</script>
JavaScript在线考试系统

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧