label标签的妙用:仿h5的placeholder功能

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #search{
            width: 300px;
            height: 20px;
            margin: 100px auto;
            position: relative;
        }
        #search_box{
            width: 200px;
            height: 20px;
        }
        #placeholder{
            position: absolute;
            left: 20px;
            top: 0px;
            color: #ccc;
            line-height: 25px;
            font-size: 12px;
            cursor: text;
        }
    </style>
</head>
<body>
<div id="search">
    <input type="text" id="search_box">
    <label for="search_box" id="placeholder">必败的国际大牌</label>
</div>
<script>
    let placeholder = document.getElementById('placeholder');
    document.getElementById('search_box').addEventListener('input',function () {
        this.value?placeholder.style.display='none':placeholder.style.display='block';
    })
</script>
</body>
</html>
posted @ 2019-12-20 11:36  杨小越  阅读(495)  评论(0)    收藏  举报