js_简单的登录焦点的获取以及改变

    当打开网页时,用户名自动获取焦点,按enter,焦点改变

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr><td>用户名</td><td><input type="text"></td></tr>
    <tr><td>密 码</td><td><input type="text"></td></tr>

</table>
</body>
<script>

    //获得当前页面的input
    var inputs = document.getElementsByTagName('input');
    //设置回车键下标
    var index = 0;
    //设置输入框获得焦点
    inputs[index].focus();

    document.onkeydown = function (ev) {
        if(ev.keyCode==13)
            index++;
        if(index==2)
            index = 0;
        inputs[index].focus();
    }

</script>
</html>

猜你喜欢

转载自blog.csdn.net/qq_34607371/article/details/81138142
今日推荐