DIV CSS 佈局教程網

兩種做法:讓輸入框裡的提示文字自動消失
編輯:關於JavaScript     

方法一:HTML5 的 placeholder

最省事的方式是直接用 HTML5 為 input 新增的 placeholder 屬性,瀏覽器會自動在輸入框裡顯示提示,一旦使用者開始打字就自己消失。順帶一提,加上 x-webkit-speech 屬性還能啟用語音輸入。

<div><input type="email" name="email" spellcheck="false" placeholder="郵 箱" autofocus tabindex="1" x-webkit-speech></div>
<div><input type="password" name="password" placeholder="密 碼" tabindex="2"></div>

這個做法的缺點是舊版瀏覽器不支援(實務上多用 placeholder 的 polyfill 補洞),而且提示文字會在 value 之外獨立顯示,無法用程式讀取,也不能客製字型樣式。

方法二:用 span 覆蓋模擬

另一條路是自己做:把提示文字寫進一個 span,用絕對定位疊在輸入框上,再靠 JS 判斷輸入框有沒有值來決定顯示或隱藏。這種做法的好處是提示內容完全可控,樣式可以自由變化。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>輸入框提示效果</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.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(){
   $(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>

兩種時機的差別

範例刻意做了兩個版本。上方表單是「聚焦型」:只要輸入框取得焦點(focus)就隱藏提示,失焦時再看有沒有值決定要不要顯示。下方表單是「輸入型」:綁 keyup,只有在真的打了字之後才隱藏,單純點進去提示還在。兩者體驗各異,可依設計稿挑選。

CSS 的關鍵在 label{position:relative} 搭配 span{position:absolute},讓提示文字精準疊在輸入框內側;再用 line-height 對齊垂直中心。

最後要提醒的是:若提示文字本身帶有語意(例如「請填寫身分證字號」),用 span 模擬時記得替 input 補上 aria-label 或改用 <label> 的 for 關聯,否則螢幕閱讀器使用者會完全听不到這個提示。今天若不是為了特殊視覺效果,直接 placeholder 加 <label> 才是首選。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved