源代码:
下载代码
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form id="myForm" action="/"> name: <input type="text" name="fname" value="RUNOOB"><br> url: <input type="text" name="lname" value="www.runoob.com"><br> <input type="submit" value="提交按钮"> </form> <p>点击按钮显示表单元素数量。</p> <button onclick="myFunction()">点我</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myForm").elements.length; document.getElementById("demo").innerHTML = "找到 " + x + " 个元素。"; } </script> </body> </html>
运行结果: