属性以下是表单集合的属性 -
属性描述
length 它是一个只读属性,返回集合中 元素的数量。
方法以下是表单收集的方法 -
方法描述
[index] 从给定索引处的集合中返回 元素。索引从 0 开始,如果索引越界,则返回 null。
item(index) 从给定索引处的集合。索引从 0 开始,如果索引超出范围,则返回 null。
nameditem(id) 从具有给定 id 的集合。如果 id 不存在,则返回 null。
语法以下是 html dom 表单集合的语法 - p>document.forms
示例让我们看一下 html dom 表单集合的示例 -
现场演示
<!doctype html><html><head><script> function formcollect() { for(var i=0;i<document.forms.length;i++){ var no=document.forms[i].id+"<br>"; document.getelementbyid("sample").innerhtml +=no; } }</script></head><body><h1>forms collection example</h1><form id="form1">fruit <input type="text" name="fname" value="mango"></form><form id="form2">age <input type="text" name="age" value="22"></form><form id="form3">password: <input type="password" name="pass" value="test"></form><br><button onclick="formcollect()">get ids</button><p id="sample">following are the form ids <br></p></body></html>
输出这将产生以下输出 -
单击“获取 ids”按钮时 -
在上面的示例中 -
我们首先创建了三个 id 为“form1”、“form2”的表单和“form3”。前两种形式具有文本类型的输入元素,第三种形式具有密码类型的输入元素 -
<form id="form1">fruit <input type="text" name="fname" value="mango" ></form><form id="form2">age <input type="text" name="age" value="22" ></form><form id="form3">password: <input type="password" name="pass" value="test"></form>
get ids 按钮在用户单击时执行 formcollect() 方法 -
<button onclick="formcollect()">get ids</button>
formcollect() 方法获取 document.forms length 属性值(在我们的例子中为 3),并在 for 循环内的测试表达式中使用它。使用表单集合上的索引号,我们获取它们的 id 并将其附加到 id 为“sample”的段落中用于显示 -
function formcollect() { for(var i=0;i<document.forms.length;i++){ var no=document.forms[i].id+"<br>"; document.getelementbyid("sample").innerhtml +=no; }}
以上就是html dom表单集合的详细内容。
