为什么只能用点表示法引用表单?

为什么只能用点表示法引用表单?

问题描述:

似乎是一个简单的初学者问题,但我无法在任何地方找到答案。

It seems like a simple beginners question, but I'm unable to find an answer anywhere.

假设我有这个HTML:

Let's say I have this HTML:

<form name="myForm">
   <input type="text" name="myTxt">
</form>

然后我可以使用以下javascript

then I can use the following javascript

document.myForm.myTxt.value = 'foo';

但是如果我有一个div而不是一个表单呢?

But what if I have a div instead of a form?

<div name="myDiv">
   <span name="mySpan"></span>
</div>

为什么我不能在这里做同样的事情,像

Why can't I do the same thing here, like

document.myDiv.mySpan.innerHTML = "bar";

似乎应该可以做,而不必使用getELementById(),但我可以

Seems like it should be possible to do, instead of having to use getELementById(), but I can't make it work.

由W3C设计。

真的没有什么可争论的。您可以通过 document.forms 访问页面上所有表单的HTMLCollection,就像您可以使用 document.images applet与 document.applets ,链接与 document.links 和锚点与 document.anchors

There's really nothing to argue about. You can access the an HTMLCollection of all the forms on the page through document.forms, much like you can images with document.images, applets with document.applets, links with document.links and anchors with document.anchors.

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-1689064