val方法
获取焦点时,判断value等不等于洋酒,true的话就情况,设置为空
$("#txt").focus(function () { //如果是默认值,清空内容 if($(this).val() === "洋酒"){ $(this).val(""); } });
失去焦点时,判断value是否为空,true就添加value值为洋酒
$("#txt").blur(function () { //如果为空,就添加value洋酒 if($(this).val() === ""){ $(this).val("洋酒"); } });
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> <style> input { background-color: rgb(255, 255, 255); border: 2px solid rgb(226, 35, 26); } #btn { background-color: rgb(225, 37, 27); 55px; height: 30px; color: rgb(255, 255, 255); font-size: 14px; } #txt { color: rgb(152, 152, 152); 190px; height: 25px; padding: 5px 10px; } </style> </head> <body> <input type="button" value="搜索" id="btn"> <input type="text" value="洋酒" id="txt"> <script src="jquery-1.12.4.js"></script> <script> $(function () { $("#txt").focus(function () { //如果是默认值,清空内容 if ($(this).val() === "洋酒") { $(this).val(""); } s }); $("#txt").blur(function () { if ("#txt".val === "") { $(this).val("洋酒"); } }); }); </script> </body> </html>