JS获得焦点时,className的边框效果无效

做一个练习想实现世纪佳缘网登录框的一个小效果
点击鼠标时,边框为粉色,字体为浅色
在这里插入图片描述

移除鼠标时,边框为浅色,字体为深色
在这里插入图片描述
修改元素的时候边框效果一直无法实现,是因为缺少了
outline:none; 给登录框(ipt)css样式增加即可

   div {
        width: 600px;
        margin: 100px auto;
    }

    .ipt {
        width: 120px;
        height: 23px;
        padding: 0 4px 0 10px;
        outline:none;
    }

    .hd {
        border: 1px solid #ffd6db;
        color: #888;
    }

    .sq {
        border: 1px #d9d9d9 solid;
        color: #333;

    }
</style>

<body>
    <div>
        <input type="text" class="ipt" value="邮箱/ID/手机号">
    </div>

    <script>
        // 1.获取元素
        var ipt = document.querySelector('.ipt');
        // 2.注册事件,获得焦点onfocus
        ipt.onfocus = function () {
            if(this.value === '邮箱/ID/手机号'){
                this.style.color = '#888'
                this.value = '';
            }
            this.className = ' ipt hd'//双类名
        }
        // onblur 失去焦点
        ipt.onblur = function () {
            if(this.value === ''){
                this.value = '邮箱/ID/手机号';
            }
            this.className = ' ipt sq';//双类名
        }
    </script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值