前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JavaScript案例:按键输入内容,模拟自动大字号

JavaScript案例:按键输入内容,模拟自动大字号

作者头像
岳泽以
发布2022-10-26 17:22:52
1.7K0
发布2022-10-26 17:22:52
举报
文章被收录于专栏:岳泽以博客岳泽以博客

模拟按键输入内容

核心思路:检测用户是否按下了s键,如果按下s键,就把光标定位到输入框里面。

使用键盘事件对象里面的keyCode判断用户按下的是否是s键

输入框获得焦点,使用js中的focus()方法

代码语言:javascript
复制
    <input type="text">
    <script>
        var input = document.querySelector('input');
        document.addEventListener('keyup', function (e) {
            if (e.keyCode == 83) {
                input.focus();
            }
        })
    </script>

模拟自动大字号

要求:当我们在文本框中输入内容时,文本框上面自动显示大字号的内容。

  • 输入内容时,上面的大号字体盒子(con)显示(这里面的字号更大)
  • 表单检测用户输入:给表单添加键盘事件
  • 同时把表单里面的值(value)获取过来赋值给con盒子(innerText)作为内容
  • 如果表单输入框里面的内容为空,则隐藏大号字体盒子(con)
  • 当我们失去了焦点,就隐藏这个con盒子
  • 当我们获得焦点,并且文本框内容不为空,就显示这个con盒子。

注意:keydown和keypress在文本框里面的特定,他们俩个事件触发的时候,文字还没有落入文本框中。keyup触发的时候,文字已经落入文本框中了。

代码语言:javascript
复制
<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .search {
            position: relative;
            width: 178px;
            margin: 100px;
        }

        .con {
            display: none;
            position: absolute;
            top: -40px;
            width: 171px;
            border: 1px solid rgba(0, 0, 0, .2);
            box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
            padding: 5px 0;
            font-size: 18px;
            line-height: 20px;
            color: #333;
        }

        .con::before {
            content: '';
            width: 0;
            height: 0;
            position: absolute;
            top: 28px;
            left: 18px;
            border: 8px solid #000;
            border-style: solid dashed dashed;
            border-color: #fff transparent transparent;
        }
    </style>
</head>
<body>
    <div class="search">
        <div class="con">123</div>
        <input type="text" placeholder="请输入内容" class="jd">
    </div>
    <script>
        var con = document.querySelector('.con');
        var jd_input = document.querySelector('.jd');
        jd_input.addEventListener('keyup', function () {
            if (this.value == '') {
                con.style.display = 'none';
            } else {
                con.style.display = 'block';
                con.innerText = this.value;
            }
        })
        jd_input.addEventListener('blur', function () {
            con.style.display = 'none';
        })
        jd_input.addEventListener('focus', function () {
            if (this.value !== '') {
                con.style.display = 'block';
            }
        })
    </script>
</body>
</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022 年 05 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 模拟按键输入内容
    • 模拟自动大字号
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档