getElementsByName通过标签的name属性来获取元素对象
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> <style> *{ font-family: 微软雅黑; } </style> </head> <body> <form action=""> <p>用户名:</p> <p><input type="text" name='username'></p> <p>密码:</p> <p><input type="text" name='password'></p> <p><input type="submit" value="Ok"></p> </form> </body> <script> usernameobj=document.getElementsByName('username'); usernameobj[0].onfocus=function(){ this.style.outlineColor='#f00'; } </script> </html>
世界上最美的风景,是自己努力的模样