標籤:text ini padding image gif http pre inpu jscript
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <title>文字框中的事件應用</title> 5 <script type="text/javascript" 6 src="Jscript/jquery-1.8.2.min.js"> 7 </script> 8 <style type="text/css"> 9 body{font-size:13px} 10 /* 元素初始狀態樣式 */11 .divInit{width:390px;height:55px;line-height:55px;padding-left:20px}12 .txtInit{border:#666 1px solid;padding:3px;background-image:url(‘Images/bg_email_input.gif‘)}13 .spnInit{width:179px;height:40px;line-height:40px;float:right;margin-top:8px;padding-left:10px;background-repeat:no-repeat}14 15 /* 元素丟失焦點樣式 */16 .divBlur{background-color:#FEEEC2}17 .txtBlur{border:#666 1px solid;padding:3px;background-image:url(‘Images/bg_email_input2.gif‘)}18 .spnBlur{background-image:url(‘Images/bg_email_wrong.gif‘)}19 20 .divFocu{background-color:#EDFFD5}/* div擷取焦點樣式 */21 .spnSucc{background-image:url(‘Images/pic_Email_ok.gif‘);margin-top:20px}/* 驗證成功時span樣式 */22 </style>23 <script type="text/javascript">24 $(function() {25 $("#txtEmail").trigger("focus");//預設時文字框擷取焦點26 27 $("#txtEmail").focus(function() {//文字框擷取焦時間點事件28 $(this).removeClass("txtBlur").addClass("txtInit");29 $("#email").removeClass("divBlur").addClass("divFocu");30 $("#spnTip").removeClass("spnBlur")31 .removeClass("spnSucc").html("請輸入您常用郵箱地址!");32 })33 34 $("#txtEmail").blur(function() {//文字框丟失焦時間點事件35 var vtxt = $("#txtEmail").val();36 if (vtxt.length == 0) {37 $(this).removeClass("txtInit").addClass("txtBlur");38 $("#email").removeClass("divFocu").addClass("divBlur");39 $("#spnTip").addClass("spnBlur").html("郵箱地址不可為空!");40 }41 else {42 if (!chkEmail(vtxt)) {//檢測郵箱格式是否正確43 $(this).removeClass("txtInit").addClass("txtBlur");44 $("#email").removeClass("divFocu").addClass("divBlur");45 $("#spnTip").addClass("spnBlur").html("郵箱格式不正確!");46 } 47 else {//如果正確48 $(this).removeClass("txtBlur").addClass("txtInit");49 $("#email").removeClass("divFocu");50 $("#spnTip").removeClass("spnBlur").addClass("spnSucc").html("");51 }52 }53 })54 /*55 *驗證郵箱格式是否正確56 *參數strEmail,需要驗證的郵箱57 */58 function chkEmail(strEmail) {59 if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(strEmail)) {60 return false;61 }62 else {63 return true;64 }65 }66 })67 </script>68 </head>69 <body>70 <form id="form1" action="#">71 <div id="email" class="divInit">郵箱:72 <span id="spnTip" class="spnInit"></span>73 <input id="txtEmail" type="text" class="txtInit" />74 </div>75 </form>76 </body>77 </html>
jQuery-文字框事件應用-判斷郵箱地址