1、getElementById() 方法:
getElementById() 方法可返回对拥有指定 ID 的第一个对象的引用。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>获取元素</title> <script> window.onload=function(){ var uname=document.getElementById("myname"); alert(uname); } </script> </head> <body> <form action="new_file.html" method="post"> 用户名:<input type="text" name="myname" size="12" id="myname"><br> 密 码:<input type="password" name="password" size="6" ><br> </body> </html>
获取值:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>获取元素</title> <script> window.onload=function(){ var uname=document.getElementById("myname"); var uValue=uname.value; alert(uValue); } </script> </head> <body> <form action="new_file.html" method="post"> 用户名:<input type="text" name="myname" size="12" id="myname"><br> 密 码:<input type="password" name="password" size="6" ><br> </body> </html>
来源:https://www.cnblogs.com/zhai1997/p/12215328.html