为什么只能用点表示法引用表单? [英] Why can only forms be referenced by dot notation?

查看:84
本文介绍了为什么只能用点表示法引用表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于为什么只能用点表示法引用表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆