HTML DOM Belge formları
Örnek
Belgedeki <form> öğelerinin sayısı:
let num = document.forms.length;
İlk <form> öğesinin kimliğini alın:
let id = document.forms[0].id;
İlk <form> öğesinin kimliğini alın:
let id = document.forms.item(0).id;
id="myCarForm" ile <form> öğesinin HTML içeriğini alın:
let html = document.forms.namedItem("myCarForm").innerHTML;
Aşağıda daha fazla örnek.
Tanım ve Kullanım
Özellik forms
, bir belgedeki tüm <form> öğelerinin bir koleksiyonunu döndürür.
Özellik forms
, bir HTMLCollection döndürür .
forms
Mülk salt okunurdur .
HTML Koleksiyonu
HTMLCollection , HTML düğümlerinin bir koleksiyonudur .
Bir koleksiyondaki düğümlere dizin numaralarıyla erişilebilir. Endeks 0'dan başlar.
Uzunluk Özelliği , koleksiyondaki öğelerin sayısını döndürür.
Sözdizimi
document.forms
Özellikler
Property | Description |
length | The number of elements in the collection. |
yöntemler
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. |
Geri dönüş değeri
Tip | Açıklama |
Nesne | Bir HTMLCollection Nesnesi . Belgedeki tüm <form> öğeleri. Kaynak kodunda göründükleri gibi sıralanır |
Daha fazla örnek
Tüm <form> öğeleri arasında dolaşın ve her formun kimliğini çıktılayın:
const forms = document.forms;
let text = "";
for (let i = 0; i < forms.length; i++) {
text += forms[i].id + "<br>";
}
Formdaki her bir öğenin değerini almak için form.elements koleksiyonunu kullanma :
const form = document.forms[0];
let text = "";
for (let i = 0; i < form.length; i++) {
text += forms.elements[i].value + "<br>";
}
Tarayıcı Desteği
document.forms
bir DOM Düzey 1 (1998) özelliğidir.
Tüm tarayıcılarda tam olarak desteklenir:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |