首页 > 语言 > JavaScript > 正文

document.all与getElementById、getElementsByName、getElementsByT

2024-05-06 14:16:26
字体:
来源:转载
供稿:网友

理解这句话可看以下:
例1(这个可以让你理解文档中哪些是对象)
  
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
  输出结果:
代码如下:
  Example Heading
  This is a paragraph. It is only a paragraph.
  Yet another paragraph.
  This final paragraph has special emphasis.
  document.all.length=18
  document.all[0]=!
  document.all[1]=HTML
  document.all[2]=HEAD
  document.all[3]=TITLE
  document.all[4]=META
  document.all[5]=BODY
  document.all[6]=H1
  document.all[7]=HR
  document.all[8]=P
  document.all[9]=EM
  document.all[10]=EM
  document.all[11]=P
  document.all[12]=EM
  document.all[13]=P
  document.all[14]=EM
  document.all[15]=EM
  document.all[16]=HR
  document.all[17]=SCRIPT

可以通过ID,NAME或INDEX属性访问某个具体的元素
例2(访问一个特定元素)
代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <title>单击DIV变色</title>
  <style type="text/css">
  <!--
  #docid{
  height:400px;
  width:400px;
  background-color:#999;}
  -->
  </style>
  </head>
  <body><div id="docid" name="docname" onClick="bgcolor()"></div>
  </body>
  </html>
  <script language="javascript" type="text/javascript">
  <!--
  function bgcolor(){
  document.all[7].style.backgroundColor="#000"
  }
  -->
  </script>
  上面的这个例子让你了解怎么访问文档中的一个特定元素,比如文档中有一个DIV
  <div id="docid" name="docname"></div>,你可以通过这个DIV的ID,NAME或INDEX属性访问这个DIV:

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选