jQuery 失去焦點時輸入框為空時自動填寫默認內容

jopen 9年前發布 | 7K 次閱讀 JavaScript jQuery

$("#address").focus(function () { // 地址框獲得鼠標焦點
    var txt_value = $(this).val(); // 得到當前文本框的值
    if (txt_value == "請輸入郵箱地址") {
        $(this).val(""); // 如果符合條件,則清空文本框內容
    }
});
$("#address").blur(function () { // 地址框失去鼠標焦點
    var txt_value = $(this).val(); // 得到當前文本框的值
    if (txt_value == "") {
        $(this).val("請輸入郵箱地址"); // 如果符合條件,則設置內容
    }
})

$("#password").focus(function () { var txt_value = $(this).val(); if (txt_value == "請輸入郵箱密碼") { $(this).val(""); } }); $("#password").blur(function () { var txt_value = $(this).val(); if (txt_value == "") { $(this).val("請輸入郵箱密碼"); } })

//另一個方法

$(function () { $("#address").focus(function () { // 地址框獲得鼠標焦點 var txt_value = $(this).val(); // 得到當前文本框的值 if (txt_value == this.defaultValue) { $(this).val(""); // 如果符合條件,則清空文本框內容 } }); $("#address").blur(function () { // 地址框失去鼠標焦點 var txt_value = $(this).val(); // 得到當前文本框的值 if (txt_value == "") { $(this).val(this.defaultValue); // 如果符合條件,則設置內容 } })

$("#password").focus(function () {
    var txt_value = $(this).val();
    if (txt_value == this.defaultValue) {
        $(this).val("");
    }
});
$("#password").blur(function () {
    var txt_value = $(this).val();
    if (txt_value == "") {
        $(this).val(this.defaultValue);
    }
})

});

</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!