jquery 失去焦点时输入框为空时自动填写默认内容

 1 $("#address").focus(function () { // 地址框获得鼠标焦点
 2     var txt_value = $(this).val(); // 得到当前文本框的值
 3     if (txt_value == "请输入邮箱地址") {
 4         $(this).val(""); // 如果符合条件,则清空文本框内容
 5     }
 6 });
 7 $("#address").blur(function () { // 地址框失去鼠标焦点
 8     var txt_value = $(this).val(); // 得到当前文本框的值
 9     if (txt_value == "") {
10         $(this).val("请输入邮箱地址"); // 如果符合条件,则设置内容
11     }
12 })
13  
14 $("#password").focus(function () {
15     var txt_value = $(this).val();
16     if (txt_value == "请输入邮箱密码") {
17         $(this).val("");
18     }
19 });
20 $("#password").blur(function () {
21     var txt_value = $(this).val();
22     if (txt_value == "") {
23         $(this).val("请输入邮箱密码");
24     }
25 })
26  
27  
28  
29  
30 //另一个方法
31  
32 $(function () {
33     $("#address").focus(function () { // 地址框获得鼠标焦点
34         var txt_value = $(this).val(); // 得到当前文本框的值
35         if (txt_value == this.defaultValue) {
36             $(this).val(""); // 如果符合条件,则清空文本框内容
37         }
38     });
39     $("#address").blur(function () { // 地址框失去鼠标焦点
40         var txt_value = $(this).val(); // 得到当前文本框的值
41         if (txt_value == "") {
42             $(this).val(this.defaultValue); // 如果符合条件,则设置内容
43         }
44     })
45      
46     $("#password").focus(function () {
47         var txt_value = $(this).val();
48         if (txt_value == this.defaultValue) {
49             $(this).val("");
50         }
51     });
52     $("#password").blur(function () {
53         var txt_value = $(this).val();
54         if (txt_value == "") {
55             $(this).val(this.defaultValue);
56         }
57     })
58 });
原文地址:https://www.cnblogs.com/chengkun101/p/5228577.html