HTML5搜索框灰色提示字、关键词

关于搜索框灰色字体聚焦是消失,失去焦点是出现,不影响搜索案例

搜索框灰色提示字,如图所示:

在这里插入图片描述
代码如下:
html代码

<body>
<div class="box">
    <input type="text" value="手机">
</div>

<script>
    //获取元素
    var input = document.querySelector('input');
    //聚焦时  搜索框中内容自动消失,字体颜色加深
    input.onfocus = function() {
        if (this.value == '手机') {
            input.value = '';
            input.style.color = 'black';
        }

    }

    //失去聚焦时  搜索框内容自动恢复
    input.onblur = function() {
        if (this.value == '') {
            input.value = '手机';
            input.style.color = '#ccc';
        }
    }
</script>
</body>

css代码如下:

.box {
        width: 1200px;
        margin: 0 auto;
    }
    
    .box input {
        display: block;
        width: 500px;
        height: 35px;
        outline: none;
        border: 1px solid #ccc;
        color: #ccc;
        margin: 0 auto;
    }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值