当前位置: 首页 > 图文教程 > 网络编程 > Javascript > document 和 document.all 分别什么时候用

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 document 和 document.all 分别什么时候用


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 254 ::
收藏到网摘: n/a

<body>
<div id="aa">123456</div>
<input type="button" value="这里用 document 就出错" onclick="alert(document.aa.innerText);" />
<br />
<input type="button" value="这里用 document.all 就不出错" onclick="alert(document.all.aa.innerText);" />
</body>
运行以上代码.

如果与a,form对象,image对象,applet对象相对应的html标记中设定了name性质,它的值将被用作document对象的属性名,用来引用相应的对象,其他的对象则不可以。
另外,input等如果作为form的子元素,则直接用inputName或者document.inputName来引用此对象就是错误的,必须使用formName.inputName引用,否则就可以使用inputName来引用.
另外应该注意到有很多平时用的元素都没有name.
如果想引用一个有id的元素,只能用Id或者document.getElementById,document.all.id来引用
但是象这样的元素,所以象<a href="......" name="linkname" id="linkid">......</a>这样的
可以用
linkid.href;
linkname.href;
document.all.linkid.href;
document.all.linkname.href;
document.getElementById("linkid").href;
document.getElementsByName("linkname")[0].href来引用

all是一个集合,包含所有html对像的集合,写一个程式,可以存取到所有的对像。像这样:
<script language="javascript">
var obj="";
for(i=0;i<document.all.length;i++)
obj+=document.all[i].tagName+";";
alert(obj);
</script>
注意要把程式放到</html>之后哦。