0
点赞
收藏
分享

微信扫一扫

显示隐藏文本框内容小案例

zibianqu 2022-03-26 阅读 64
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;
        }
        
        input {
            color: #ccc;
        }
    </style>
</head>

<body>
    <input type="text" value="手机" />
    <script>
        //获取元素
        var ipt = document.querySelector("input");
        //注册事件 获得焦点事件 onfocus
        ipt.onfocus = function() {
            if (this.value === "手机") {
                this.value = "";
            }
            //获得焦点需把文本框里的颜色变黑
            this.style.color = "#333";
        };
        //注册事件 失去焦点事件
        ipt.onblur = function() {
            if (this.value === "") {
                this.value = "手机";
            }
            //失去焦点需把文本框的颜色变浅
            this.style.color = "#ccc";
        };
    </script>
</body>

</html>
举报

相关推荐

0 条评论