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>

 

posted @ 2020-12-27 22:30  大熊童鞋  阅读(279)  评论(0编辑  收藏  举报