美文网首页
原生js兼容ie9 placeholder

原生js兼容ie9 placeholder

作者: nzjcnjzx | 来源:发表于2019-08-06 09:22 被阅读0次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            position: relative;
            width: 220px;
            height: 32px;
            font-size: 16px;
            line-height: 16px;
        }

        .input {
            width: 100%;
            height: 100%;
            padding-left: 3px;
            font-size: 16px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            outline: none;
        }

        .place-holder {
            position: absolute;
            left: 3px;
            top: 50%;
            margin-top: -8px;
            color: #999;
        }

        .default {
            width: 220px;
            height: 32px;
            box-sizing: border-box;
            font-size: 16px;
        }

        ::-webkit-input-placeholder {
            color: #999;
        }
    </style>
</head>

<body>
    <div class="box">
        <input type="text" class="input">
        <span class="place-holder">请输入关键字</span>
    </div>
    <div style="margin-top: 20px;">
        <input class="default" type="text" placeholder="请输入关键字">
    </div>
</body>

</html>
<script>
    var oSpan = document.querySelector('.place-holder');
    var oInput = document.querySelector('.input');
    oSpan.onclick = function() {
        oInput.focus();
    }
    oInput.oninput = function() {
        if (oInput.value) {
            oSpan.style.display = 'none';
        } else {
            oSpan.style.display = 'inline-block';
        }
    }
    oInput.onblur = function() {
        if (oInput.value) {
            oSpan.style.display = 'none';
        } else {
            oSpan.style.display = 'inline-block';
        }
    }
</script>

相关文章

网友评论

      本文标题:原生js兼容ie9 placeholder

      本文链接:https://www.haomeiwen.com/subject/hggbdctx.html