this.form和document.forms之间的区别 [英] Difference between this.form and document.forms

查看:261
本文介绍了this.form和document.forms之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

this.formdocument.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屋!

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