Heim  >  Artikel  >  Web-Frontend  >  document 和 document.all 分别什么时候用_JavaScript

document 和 document.all 分别什么时候用_JavaScript

WBOY
WBOYOriginal
2016-05-16 19:29:25970Durchsuche
 
body>
div id="aa">123456div>
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来引用
但是象这样的元素,所以象......这样的
可以用
linkid.href;
linkname.href;
document.all.linkid.href;
document.all.linkname.href;
document.getElementById("linkid").href;
document.getElementsByName("linkname")[0].href来引用

all是一个集合,包含所有html对像的集合,写一个程式,可以存取到所有的对像。像这样:

注意要把程式放到

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn