日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文本框输入值文字消失常用的两种方法

發布時間:2023/12/9 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文本框输入值文字消失常用的两种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.這種相對來說較簡單,舉例子:

<input name="textfield" type="text" value="點擊添入標題" style="color: gray;" onfocus="if (value =='點擊添入標題'){value =''}" onblur="if (value ==''){value='點擊添入標題'}" />

注:onfocus事件在對象獲得焦點時發生;onblur 事件會在對象失去焦點時發生。

2.另外一種復雜一些,用jquery,直接貼代碼細看:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>文本框輸入值文字消失</title> <script src="js/jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){$("#focus .input_txt").each(function(){var thisVal=$(this).val();//判斷文本框的值是否為空,有值的情況就隱藏提示語,沒有值就顯示if(thisVal!=""){$(this).siblings("span").hide();}else{$(this).siblings("span").show();}//聚焦型輸入框驗證 $(this).focus(function(){$(this).siblings("span").hide();}).blur(function(){var val=$(this).val();if(val!=""){$(this).siblings("span").hide();}else{$(this).siblings("span").show();} });});$("#keydown .input_txt").each(function(){var thisVal=$(this).val();//判斷文本框的值是否為空,有值的情況就隱藏提示語,沒有值就顯示if(thisVal!=""){$(this).siblings("span").hide();}else{$(this).siblings("span").show();}$(this).keyup(function(){var val=$(this).val();$(this).siblings("span").hide();}).blur(function(){var val=$(this).val();if(val!=""){$(this).siblings("span").hide();}else{$(this).siblings("span").show();}})});}); </script> <style type="text/css">form{width:400px;margin:10px auto;border:solid 1px #E0DEDE;background:#FCF9EF;padding:30px;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;}label{display:block;height:40px;position:relative;margin:20px 0;}span{position:absolute;float:left;line-height:40px;left:10px;color:#BCBCBC;cursor:text;}.input_txt{width:398px;border:solid 1px #ccc;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;height:38px;text-indent:10px;}.input_txt:focus{box-shadow:0 0 4px rgba(255,153,164,0.8);border:solid 1px #B00000;}.border_radius{border-radius:5px;color:#B00000;}h2{font-family:"微軟雅黑";text-shadow:1px 1px 3px #fff;} </style> </head> <body> <form class="border_radius" id="focus"><h2>聚焦型提示語消失</h2><label><span>花瓣注冊郵箱</span><input type="text" class="input_txt border_radius" /></label><label><span>密碼</span><input type="text" class="input_txt border_radius" /></label></form><form class="border_radius" id="keydown"><h2>輸入型提示語消失</h2><label><span>花瓣注冊郵箱</span><input type="text" class="input_txt border_radius" /></label><label><span>密碼</span><input type="text" class="input_txt border_radius" /></label> </form> </body> </html>

?

轉載于:https://www.cnblogs.com/anns/p/4727279.html

總結

以上是生活随笔為你收集整理的文本框输入值文字消失常用的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。