欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實現(xiàn)input輸入框獲取焦點與失去焦點時提示的消失與顯示功能示例

 更新時間:2019年05月27日 11:31:02   作者:輕舞肥羊  
這篇文章主要介紹了jQuery實現(xiàn)input輸入框獲取焦點與失去焦點時提示的消失與顯示功能,涉及jQuery事件響應及頁面元素屬性動態(tài)操作相關實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了jQuery實現(xiàn)input輸入框獲取焦點與失去焦點時提示的消失與顯示功能。分享給大家供大家參考,具體如下:

最近都成為頁面仔了,主要工作都放在了前段,以前總是寫后臺程序,對前端的一些技術 html,css,javascript ,雖然都懂一些,但要做出比較好看頁面,還是有很大的差距的。最近就遇到了這樣一個要求不是很高,但有點小清新風格的登錄或注冊頁面,要求如下:

1. 在輸入框中 如果沒有內(nèi)容,則顯示提示:比如"請輸入用戶名"
2. 如果輸入框獲得焦點,則隱藏提示
3. 如果輸入框失去焦點,并且輸入框沒有內(nèi)容,則顯示提示,如果有內(nèi)容,則隱藏提示。
4. 采用 Jquery 1.7.2

在搜索了資料之后,發(fā)現(xiàn)通過label, input 并結合javascript 結合來實現(xiàn),因為 label 有一個 for 屬性,并指向input 的id ,這樣,只要點擊 label ,input 輸入框就能獲取焦點.一旦獲取焦點就響應Javascript事件。隱藏label. 同樣在失去焦點的時候,也觸發(fā)事件,判斷輸入框是否有內(nèi)容,來確定是否顯示提示。整個效果如下:

獲取焦點后

代碼如下:

<!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>www.dbjr.com.cn jQuery input焦點與提示文字</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#loginform .input_txt").each(function(){
   var thisVal=$(this).val();
   //判斷文本框的值是否為空,有值的情況就隱藏提示語,沒有值就顯示
   if(thisVal!=""){
    $(this).siblings("label").hide();
   }else{
    $(this).siblings("label").show();
   }
   //聚焦型輸入框驗證
   $(this).focus(function(){
    $(this).siblings("label").hide();
   }).blur(function(){
    var val=$(this).val();
    if(val!=""){
     $(this).siblings("label").hide();
    }else{
     $(this).siblings("label").show();
    }
   });
  })
 })
</script>
<style type="text/css">
form{width:400px;margin:10px auto;border:solid 1px #E0fEDE;background:#FCF9EF;padding:30px;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;}
span{display:block;height:40px;position:relative;margin:20px 0;}
label{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;}
</style>
</head>
<body>
<form class="border_radius" id="loginform">
    www.dbjr.com.cn得到焦點時提示語消失
    <span>
       <label for="username">請輸入賬號</label>
       <input type="text" class="input_txt border_radius" id="username" />
    </span>
    <span>
       <label for="password">密碼</label>
       <input type="text" class="input_txt border_radius" id="password" />
    </span>
</form>
</body>
</html>

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具 http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。

更多關于jQuery相關內(nèi)容感興趣的讀者可查看本站專題:《jQuery form操作技巧匯總》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery常用插件及用法總結》、《jQuery擴展技巧總結》、《jQuery表格(table)操作技巧匯總》及《jquery選擇器用法總結

希望本文所述對大家jQuery程序設計有所幫助。

相關文章

最新評論