帮我分析一下原因

csdnquestion 2009-11-14 12:30:25
一个html文件中内容如下:

<script language="javascript">
document.write("anchors长度:");
document.write(document.anchors.length);
document.write("所有长度:");
document.write(document.all.length);
</script>
<form>
<body>
<a href="#mark1" name=href1 id="p1">to p1</a><br>
<a href="#mark2" name=href2 id="p2">to p1</a><br>
<a name="mark1" id="p3">paragraph1<br>
<a name="mark2" id="p4">paragraph2<br>
</body>
</form>


我运行时显示:

anchors长度:0所有长度:5
to p1
to p1
paragraph1
paragraph2


为什么anchors的长度为0,所有的长度为5?
...全文
39 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohighthesky 2009-11-14
  • 打赏
  • 举报
回复
把这个加到你script的标签后面,运行下就知道了,
然后再把script标签移到body后面再执行一遍
因为你下面的标签还没加载呢?要放在onload中才行

for(var i=0;i<document.all.length;i++)
document.write( document.all[i].nodeName);
jianguangguo 2009-11-14
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<form>
<body onload="a()">
<a href="#mark1" name=href1 id="p1">top1 </a> <br>
<a href="#mark2" name=href2 id="p2">top1 </a> <br>
<a name="mark1" id="p3">paragraph1 </a><br>
<a name="mark2" id="p4">paragraph2 </a><br>
</body>
</form>
<script language="javascript">
document.write("anchors长度:");
document.write(document.anchors.length);
document.write("所有长度:");
document.write(document.all.length);
for(var i=0;i<document.all.length;i++)
document.write( document.all[i].nodeName);

</script>
</html>

61,115

社区成员

发帖
与我相关
我的任务
社区描述
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
社区管理员
  • HTML(CSS)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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