JavaScript基础学习 模拟京东查询快递单号

JavaScript基础学习 模拟京东查询快递单号

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=devcie-width,initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            height: 50px;
            width: 500px;
            background-color: pink;
            font-size: 50px;
            display: none;
        }
    </style>

</head>

<body>
    <div>
        <div class="box"></div><br>
        <input type="text" placeholder="请输入你的快递单号" id="jd">
    </div>
    <script>
        var input = document.querySelector('input');
        var box = document.querySelector('.box');
        input.addEventListener('keyup', function() {
            if (this.value == '') {
                box.style.display = 'none';
            } else {
                box.style.display = 'block';
                box.innerHTML = this.value;
            }

        })

        // 当我们失去焦点时,就隐藏输入框上方的盒子
        input.addEventListener('blur', function() {
            box.style.display = 'none';
        })

        // 当我们获得焦点时,就显示输入框上方的盒子
        input.addEventListener('focus', function() {
            if (this.value == '') {
                box.style.display = 'none';
            } else {
                box.style.display = 'block';
            }

        })
    </script>

</body>

</html>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43537319/article/details/122205515