前端js實踐——輸入框內容的顯示和隱藏


主要內容:Javascrip中事件處理的方法,DOM中的常見對象及其屬性、方法;

題目描述:顯示和隱藏輸入框中的提示內容,具體表現如下圖:

1.輸入框獲得焦點,提示內容消失,邊框變色

    

2.輸入框失去焦點,如果內容為空,提示內容恢復,邊框變色;如果內容不為空,只有邊框變色

   

 

 代碼:<!DOCTYPE html>
<html>
<head>
    <meta charset="UFT-8">
    <meta name = "viewport" content="width = device-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 = "郵箱/ID/手機號">    //框內文本
    <script>

       //文檔對象模型Document引用的querySelector()方法返回文檔中與指定選擇器或選擇器組匹配的第一個 html元素Element。 如果找不到匹配項,則返回null
        var text = document.querySelector("input");   
        text.onfocus = function(){
            if(this.value === "郵箱/ID/手機號")     //獲得焦點
            {
                this.value = '';
            } 
        }
        text.onblur = function(){       //失去焦點
            if(this.value === '' )
            {
                this.value = "郵箱/ID/手機號";
            }
        }
    </script>
</body>
<ml>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM