IE下實現html5中的placeholder

IE不支持input標籤中html5的placeholder屬性,上網找了一下解決方法,感覺還不錯。

可以封裝成一個js文件,然後調用。

 

  1. $(function() {    
  2.            $("input").inputTip();  // 調用inputTip方法    
  3.            $("input[type='button']").focus();  // 頁面打開後焦點置於button上,也可置於別處。否則IE上刷新頁面後焦點在第一個輸入框內造成placeholder文字後緊跟光標現象 
  4.     }); 
  5.      
  6.     $.fn.extend({ 
  7.     /** 
  8.      * 該方法爲了解決不支持placeholder屬性的瀏覽器下達到相似效果作用 
  9.      * @param _color 文本框輸入字體顏色,默認黑色 
  10.      * @param _plaColor 文本框placeholder字體顏色,默認灰色#a3a3a3 
  11.      */ 
  12.     inputTip:function(_color, _plaColor) { 
  13.         _color = _color || "#000000"
  14.         _plaColor = _plaColor || "#a3a3a3"
  15.         function supportsInputPlaceholder() { // 判斷瀏覽器是否支持html5的placeholder屬性 
  16.             var input = document.createElement('input'); 
  17.             return "placeholder" in input; 
  18.         } 
  19.     
  20.         function showPassword(_bool, _passEle, _textEle) { // 密碼框和文本框的轉換 
  21.             if (_bool) { 
  22.                 _passEle.show(); 
  23.                 _textEle.hide(); 
  24.             } else { 
  25.                 _passEle.hide(); 
  26.                 _textEle.show(); 
  27.             } 
  28.         } 
  29.     
  30.         if (!supportsInputPlaceholder()) { 
  31.             this.each(function() { 
  32.                 var thisEle = $(this); 
  33.                 var inputType = thisEle.attr("type"
  34.                 //alert(inputType); 
  35.                 var isPasswordInput = inputType == "password"
  36.                 var isInputBox = inputType == "password" || inputType == "text"
  37.                 //alert("判斷是否爲密碼或文本====="+isInputBox); 
  38.                 if (isInputBox) { //如果是密碼或文本輸入框 
  39.                     var isUserEnter = false// 是否爲用戶輸入內容,允許用戶的輸入和默認內容一樣 
  40.                     var placeholder = thisEle.attr("placeholder"); 
  41.                     //alert("判斷是否爲密碼框======="+isPasswordInput); 
  42.                     if (isPasswordInput) { // 如果是密碼輸入框 
  43.                         //原理:由於input標籤的type屬性不可以動態更改,所以要構造一個文本input替換整個密碼input 
  44.                         var textStr = "<input type='text' class='" + thisEle.attr("class") + "' style='" + (thisEle.attr("style") || "") + "' />"
  45.                         var textEle = $(textStr); 
  46.                         textEle.css("color", _plaColor).val(placeholder).focus( 
  47.                             function() { 
  48.                                 thisEle.focus(); 
  49.                             }).insertAfter(this); 
  50.                         thisEle.hide(); 
  51.                     } 
  52.                     thisEle.css("color", _plaColor).val("");//解決ie下刷新無法清空已輸入input數據問題 
  53.                     if (thisEle.val() == "") { 
  54.                         thisEle.val(placeholder); 
  55.                     } 
  56.                     thisEle.focus(function() { 
  57.                         if (thisEle.val() == placeholder && !isUserEnter) { 
  58.                             thisEle.css("color", _color).val(""); 
  59.                             //alert("focus===="+isPasswordInput); 
  60.                             if (isPasswordInput) { 
  61.                                 showPassword(true, thisEle, textEle); 
  62.                             } 
  63.                         } 
  64.                     }); 
  65.                     thisEle.blur(function() { 
  66.                         if (thisEle.val() == "") { 
  67.                             thisEle.css("color", _plaColor).val(placeholder); 
  68.                             isUserEnter = false
  69.                             //alert("blur===="+isPasswordInput); 
  70.                             if (isPasswordInput) { 
  71.                                 showPassword(false, thisEle, textEle); 
  72.                             } 
  73.                         } 
  74.                     }); 
  75.                     thisEle.keyup(function() { 
  76.                         if (thisEle.val() != placeholder) { 
  77.                             isUserEnter = true
  78.                         } 
  79.                     }); 
  80.                 } 
  81.             }); 
  82.         } 
  83.     } 
  84. }); 

 

下面是頁面的代碼,body部分:

 

  1. <div class="mt60 ml28" >
  2. <input type="text"  class="txt" id = "username" placeholder="請輸入用戶名"/>
  3. </div> 
  4. <div class="mt12 ml28">
  5. <input type="password" class="txt" id="password" placeholder="請輸入密碼"/>
  6. </div> 
  7. <div class="mt12 ml28">
  8. <input type="text" class="ytxt vt mr5" id = "valicode" placeholder="請輸入驗證碼"/> 
  9. </div>

結果表現爲:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章