欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實現(xiàn)顯示隱藏表單文字

 更新時間:2021年09月28日 08:31:41   作者:qq_39111074  
這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)顯示隱藏表單文字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了JavaScript實現(xiàn)顯示隱藏表單文字的具體代碼,供大家參考,具體內(nèi)容如下

實現(xiàn)思路

運(yùn)用 onfocus、onblur 事件

onfocus- - -獲取焦點(diǎn)(鼠標(biāo)點(diǎn)擊輸入框,輸入框里面有閃動的光標(biāo))

onblur- - -失去焦點(diǎn)(鼠標(biāo)不選中輸入框,輸入框里面失去閃動的光標(biāo))

1、給輸入框設(shè)置一個默認(rèn)值

2、獲取輸入框?qū)ο?,給其綁定事件:onfocus 和 onblur
當(dāng)獲取焦點(diǎn)時(onfocus)- - -判斷輸入框的value值是否是默認(rèn)值,如果是默認(rèn)值初始值,將value改為空,默認(rèn)初始值就沒有了,可以輸入自己的
當(dāng)時去焦點(diǎn)時(onblur)- - -判斷輸入框的value值是否沒有值,沒有的話,給value賦值默認(rèn)值,未輸入內(nèi)容移開后又會顯示默認(rèn)值了

3、還可以給獲取焦點(diǎn)和失去焦點(diǎn)兩種狀態(tài)的文字顏色設(shè)置不同,讓兩種狀態(tài)區(qū)分的更明顯

示例1:

代碼:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>獲取/失去焦點(diǎn)</title>
    <style>
        input {
            color: #ccc;
            outline: none;
        }
    </style>
</head>

<body>
    <input type="text" value="手機(jī)">
    <script>
        var text = document.querySelector('input');
        text.onfocus = function() {
            if (this.value === '手機(jī)') {
                this.value = '';
            }
            this.style.color = '#333';
        }

        text.onblur = function() {
            if (this.value === '') {
                this.value = '手機(jī)';
            }
            this.style.color = '#ccc';
        }
    </script>
</body>

</html>

頁面效果:

示例2

代碼:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用戶名 顯示 隱藏</title>
    <style>
        input {
            font-size: 14px;
            color: #999;
            outline: none;
            padding: 3px 0 3px 10px;
            border: 1px solid #ccc;
        }
    </style>
</head>

<body>
    <!--用戶名 顯示隱藏內(nèi)容 -->
    <input type="text" value="郵箱/ID/手機(jī)號" class="userName">
    <script>
        var userName = document.querySelector('.userName');
        userName.onfocus = function() {
            if (this.value === '郵箱/ID/手機(jī)號') {
                this.value = '';
                this.style.borderColor = 'pink';
            } else {
                this.style.borderColor = 'pink';
                this.style.color = '#999';
            }
        }
        userName.onblur = function() {
            if (this.value === '') {
                this.value = '郵箱/ID/手機(jī)號';
                this.style.borderColor = '#ccc';
                this.style.color = '#999';
            } else {
                this.style.borderColor = '#ccc';
                this.style.color = '#333';
            }
        }
    </script>
</body>

</html>

頁面效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論