javascript裏的document.all用法

理解document.all[]

  從IE4開始IE的object model才增加了document.all[],來看看document.all[]的Description:
Array of all HTML tags in the document.Collection of all elements contained by the object.
  也就是說document.all[]是文檔中所有標籤組成的一個數組變量,包括了文檔對象中所有元素(見例1)。

  IE’s document.all collection exposes all document elements.This array provides access to every element in the document.
  document.all[]這個數組可以訪問文檔中所有元素。
當然也可以這樣用:
<input type="button" value="Align Left"
οnclick="document.all['heading1'].align='left';" />
<input type="button" value="Align Center"
οnclick="document.all['heading1'].align='center';" />
<input type="button" value="Align Right"
οnclick="document.all['heading1'].align='right';" />

例1(這個可以讓你理解文檔中哪些是對象)
<!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>
<title>Document.All Example</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<h1>Example Heading</h1>
<hr />
<p>This is a <em>paragraph</em>. It is only a <em>paragraph.</em></p>
<p>Yet another <em>paragraph.</em></p>
<p>This final <em>paragraph</em> has <em id="special">special emphasis.</em></p>
<hr />
<script type="text/javascript">
<!--
var i,origLength;
origLength = document.all.length;
document.write('document.all.length='+origLength+"<br />");
for (i = 0; i < origLength; i++)
{
document.write("document.all["+i+"]="+document.all[i].tagName+"<br />");
}
//-->
</script>
</body>
</html>
輸出結果:
document.all.length=394
document.all[0]=!
document.all[1]=HTML
document.all[2]=HEAD
document.all[3]=TITLE
document.all[4]=META
document.all[5]=META
document.all[6]=META
document.all[7]=META
document.all[8]=META
document.all[9]=META
document.all[10]=LINK
document.all[11]=LINK
document.all[12]=LINK
document.all[13]=!
document.all[14]=META
document.all[15]=META
document.all[16]=META
document.all[17]=META
document.all[18]=META
document.all[19]=META
document.all[20]=META
document.all[21]=!
document.all[22]=BODY
document.all[23]=FORM
document.all[24]=DIV
document.all[25]=INPUT
document.all[26]=!
document.all[27]=STYLE
document.all[28]=SCRIPT
 .........................................
..........................................
document.all[388]=EM
document.all[389]=EM
document.all[390]=DIV
document.all[391]=HR
document.all[392]=P
document.all[393]=SCRIPT
 
發佈了18 篇原創文章 · 獲贊 2 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章