this.form和document.forms之间的区别 [英] Difference between this.form and document.forms
问题描述
this.form
和document.forms (document["forms"])
之间是否存在区别?或者,它们是否相似?
Is there a difference between this.form
and document.forms (document["forms"])
or, are they similar?
这是我为测试差异而编写的代码.
Here is the code I wrote to test the difference.
<form name="myForm" id="myForm">
<input type="text" name="haha" id="myForm" value="laughable" onclick="alert(this.form.haha.value)" />
</form>
alert(document.forms.myForm.haha.value);
它们都导致相同的结果.
They both result in the same thing.
推荐答案
this.form
将为您提供form元素的形式. (this
是表单元素)
this.form
will give you the form of the form element. (this
is the form element)
包含的form元素(如果此元素为表单).
The containing form element, if this element is in a form.
document.forms
将为您提供文档中的所有表格(如果支持的话!)
document.forms
will give you all the forms in the document (if it's supported!)
forms返回当前文档中表单元素的集合(HTMLCollection).
forms returns a collection (an HTMLCollection ) of the form elements within the current document.
更好地使用document.getElementById(id)
var form = document.getElementById(formId);
这篇关于this.form和document.forms之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!