文本框获得焦点

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6     <style>
 7         div {
 8             margin-bottom: 20px;
 9         }
10 
11         fieldset {
12             width: 300px;
13         }
14     </style>
15 </head>
16 <body>
17 <fieldset>
18     <legend>用户登录</legend>
19     <form action="" method="">
20         <div><label for="name">用户名</label>
21             <input type="text" id="name"><br>
22         </div>
23         <div><label for="pass">&nbsp;</label>
24             <input type="text" id="pass">
25         </div>
26         <input type="submit" value="登录" id="login"
27                style="display: block;
28                       margin-left:100px">
29     </form>
30 </fieldset>
31 <script type="text/javascript">
32     function $$(id) {
33         return document.getElementById(id);
34     }
35     function check() {
36         var $n = $$("name");
37         var $p = $$("pass");
38         if ($n.value == "") {
39             alert("请输入用户名!");
40             $n.focus();
41             return false;
42         }
43         if ($p.value == "") {
44             alert("请输入密码!");
45             $p.focus();
46             return false;
47         }
48     }
49     $$("login").onclick = function () {
50         return check();
51     }
52 </script>
53 </body>
54 </html>
原文地址:https://www.cnblogs.com/zfj-world/p/4771396.html