输入框--仿placeholder,oninput检测用户输入事件

87 篇文章 0 订阅
27 篇文章 0 订阅

user-select: none; /* 禁止选择网页内容 */
完美解决了 和input相配合的label能够选中 问题。
代码中注释部分也可以达到 此效果!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .search{
            width: 300px;
            height: 30px;
            margin: 100px auto;
            position: relative;
        }
        .search input{
            width: 200px;
            height: 20px;
            outline-style: none;
            text-indent: 10px;
        }
        .search label{
            position: absolute;
            top: 8px;
            left:10px;
            font-size: 12px;
            color: #666;
            cursor: text;
            /* touch-action: none;  禁止触发默认的手势操作
            -ms-touch-action: none; 
            pointer-events: none; css3属性,鼠标事件失效  */

            -moz-user-select: none; /*火狐*/
            -webkit-user-select: none; /*webkit浏览器*/
            -ms-user-select: none; /*IE10*/
            user-select: none;  /* 禁止选择网页内容 */
        }
    </style>
    <script>    
        window.onload = function(){
            function $(id){ return document.getElementById(id);}
             //oninput 大部分浏览器支持  检测用户表单输入内容
            //onpropertychange  ie678  检测用户表单输入内容
            $('txt').oninput = $('txt').onpropertychange = function(){  //  用户输入触发
                if($('txt').value == ""){
                    $('txtLabel').style.display = "block";
                }else{
                    $('txtLabel').style.display = "none";
                }
            }
            $('txt').onmouseover = function(){
                this.select(); //选中所有文本
            }
        }   
    </script>
</head>
<body>
    <div class="search">
        <input type="text" id="txt">
        <label for="txt" id="txtLabel">必败的品牌</label>
    </div>
</body>
</html> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值