JS——获取元素的值(getElementById() 方法)

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>&nbsp;&nbsp;&nbsp;码:<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>&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" size="6" ><br>
    </body>
</html>

猜你喜欢

转载自www.cnblogs.com/zhai1997/p/12215328.html