顯示隱藏文本框內容(56)

 


<!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>
        input {
            color: #999;
        }
    </style>
</head>

<body>
    <input type="text" value="手機">
    <script>
        // 1.獲取元素
        var text = document.querySelector('input');
        // 2.註冊事件 獲得焦點事件 onfocus 
        text.onfocus = function() {
                // console.log('得到了焦點');
                if (this.value === '手機') {
                    this.value = '';
                }
                // 獲得焦點需要把文本框裏面的文字顏色變黑
                this.style.color = '#333';
            }
            // 3. 註冊事件 失去焦點事件 onblur
        text.onblur = function() {
            // console.log('失去了焦點');
            if (this.value === '') {
                this.value = '手機';
            }
            // 失去焦點需要把文本框裏面的文字顏色變淺色
            this.style.color = '#999';
        }
    </script>

</body>

</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章