下面的JS代碼通過document.forms數組獲得網頁中表單(form)的數量
1 2 3 4 5 6 7 8 9 10 11 12 13 <!DOCTYPE html> <html> <body> <h1>sharejs.com</h1> <form name="Form1"></form> <form name="Form2"></form> <form></form> <p>Number of forms: <script> document.write(document.forms.length); </script></p> </body> </html>運行結果如下:
1 Number of forms: 3