萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

jQuery 實現輸入框焦點/輸入時提示文字顯示隱藏效果

兩種交互模式對比

模式觸發隱藏觸發顯示適用場景
聚焦模式focus 時blur 且值為空登錄/註冊表單,提示固定
輸入模式keyup 時blur 且值為空搜索框、即時提示

完整實現代碼

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>輸入框提示效果</title>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <style>
        form { max-width: 400px; margin: 20px auto; padding: 20px; border: 1px solid #e0e0e0; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .field { position: relative; margin: 15px 0; }
        .field label { display: block; margin-bottom: 5px; font-weight: 500; }
        .input-txt { width: 100%; height: 40px; padding: 0 12px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; font-size: 14px; transition: border-color 0.2s, box-shadow 0.2s; }
        .input-txt:focus { border-color: #e91e63; box-shadow: 0 0 0 3px rgba(233,30,99,0.1); outline: none; }
        .hint { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: #999; pointer-events: none; transition: opacity 0.2s, transform 0.2s; font-size: 14px; }
        .input-txt:placeholder-shown + .hint { opacity: 1; }
        .input-txt:focus + .hint,
        .input-txt:not(:placeholder-shown) + .hint { opacity: 0; transform: translateY(-50%) scale(0.9); }
    </style>
</head>
<body>
    <form id="focusForm">
        <h2>聚焦隱藏模式</h2>
        <div class="field">
            <label>郵箱</label>
            <input type="email" class="input-txt" placeholder=" ">
            <span class="hint">請輸入常用郵箱</span>
        </div>
        <div class="field">
            <label>密碼</label>
            <input type="password" class="input-txt" placeholder=" ">
            <span class="hint">6-20 位字符</span>
        </div>
    </form>

    <form id="inputForm">
        <h2>輸入隱藏模式</h2>
        <div class="field">
            <label>搜索關鍵詞</label>
            <input type="text" class="input-txt" placeholder=" ">
            <span class="hint">輸入關鍵詞搜索</span>
        </div>
    </form>

    <script>
        // 現代 CSS 方案已自動處理,無需 JS
        // 以下為兼容舊瀏覽器的 JS 降級方案
        function initPlaceholderFallback() {
            if ('placeholder' in document.createElement('input')) return; // 原生支援
            
            $('.input-txt').each(function() {
                const $input = $(this);
                const $hint = $input.next('.hint');
                const placeholder = $input.attr('placeholder');
                
                function toggleHint() {
                    $hint.toggle($input.val() === '');
                }
                
                $input.on('focus keyup', () => $hint.hide())
                      .on('blur', toggleHint);
                
                toggleHint();
            });
        }
        
        $(initPlaceholderFallback);
    </script>
</body>
</html>

現代最佳實踐:原生 placeholder + CSS

現代瀏覽器完美支援 placeholder 屬性,配合 CSS 偽類即可實現零 JS 方案:




/* CSS 控制提示顯示邏輯 */
.input-txt:placeholder-shown + .hint { opacity: 1; }
.input-txt:focus + .hint,
.input-txt:not(:placeholder-shown) + .hint { opacity: 0; }

進階:浮動標籤效果


.field { position: relative; }
.input-txt { padding-top: 18px; padding-bottom: 6px; }
.hint { 
    top: 18px; left: 12px; transform: none; 
    transition: all 0.2s ease; font-size: 14px; color: #999;
}
.input-txt:focus + .hint,
.input-txt:not(:placeholder-shown) + .hint {
    top: 2px; font-size: 11px; color: #e91e63; font-weight: 500;
}

✅ 無 JS 依賴、動畫流暢、無障礙友好

copyright © 萬盛學電腦網 all rights reserved