js开发 模仿京东按键s 立刻跳到搜索框功能

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            height: 4000px;
        }
    </style>
</head>
<body>
    <input type="text">
    <script>
        // 获取元素
        var search = document.querySelector('input');
        // 监听元素事件
        document.addEventListener('keyup',function(e){
            // 判断 键盘是否按的s键  83是返回的ASCLL码值
            if(e.keyCode === 83){
                // 给文本框设置获得焦点
                search.focus();
            }
        })
    </script>
</body>
</html>

 

posted @ 2022-01-20 19:59  嘉琦  阅读(115)  评论(0)    收藏  举报