HTML DOM 문서 양식
예시
문서의 <form> 요소 수:
let num = document.forms.length;
첫 번째 <form> 요소의 ID를 가져옵니다.
let id = document.forms[0].id;
첫 번째 <form> 요소의 ID를 가져옵니다.
let id = document.forms.item(0).id;
id="myCarForm"인 <form> 요소의 HTML 콘텐츠를 가져옵니다.
let html = document.forms.namedItem("myCarForm").innerHTML;
아래에 더 많은 예가 있습니다.
정의 및 사용
이 forms
속성은 문서에 있는 모든 <form> 요소의 컬렉션을 반환합니다.
forms
속성은 HTMLCollection 을 반환합니다 .
속성 은 forms
읽기 전용입니다.
HTML컬렉션
HTMLCollection 은 HTML 노드의 모음입니다 .
컬렉션의 노드는 인덱스 번호로 액세스할 수 있습니다. 인덱스는 0에서 시작합니다.
length 속성 은 컬렉션의 요소 수를 반환합니다.
통사론
document.forms
속성
Property | Description |
length | The number of elements in the collection. |
행동 양식
Method | Description |
[index] | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
item(index) | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
namedItem(id) | Returns the element with the specified id. Returns null if the id does not exist. |
반환 값
유형 | 설명 |
물체 | HTMLCollection 개체 . 문서의 모든 <form> 요소. 소스 코드에 나타나는 대로 정렬됨 |
더 많은 예
모든 <form> 요소를 반복하고 각 양식의 ID를 출력합니다.
const forms = document.forms;
let text = "";
for (let i = 0; i < forms.length; i++) {
text += forms[i].id + "<br>";
}
form.elements 컬렉션 을 사용하여 양식의 각 요소 값 가져오기 :
const form = document.forms[0];
let text = "";
for (let i = 0; i < form.length; i++) {
text += forms.elements[i].value + "<br>";
}
브라우저 지원
document.forms
DOM 레벨 1(1998) 기능입니다.
모든 브라우저에서 완벽하게 지원됩니다.
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |