placeholder支持ie

来源:互联网 发布:和讯网股票数据接口 编辑:程序博客网 时间:2024/05/29 18:26

很久之前写了这个插件,基于jQuery实现的,主要用于IE下实现placeholder效果,可同时支持文本和密码输入框。

placeholder是HTML5新增的一个属性,当input设置了该属性后,该值的内容将作为灰色提示显示在文本框中,当文本框获得焦点时,提示文字消失。

下载地址:http://download.csdn.net/detail/cwqcwk1/5676563(含demo)

实现代码如下:

[javascript] view plaincopy
  1. (function($) {  
  2.   /** 
  3.    * 没有开花的树 
  4.    * http://blog.csdn.net/mycwq/ 
  5.    * 2012/11/28 15:12 
  6.    */  
  7.   
  8.   var placeholderfriend = {  
  9.     focus: function(s) {  
  10.       s = $(s).hide().prev().show().focus();  
  11.       var idValue = s.attr("id");  
  12.       if (idValue) {  
  13.         s.attr("id", idValue.replace("placeholderfriend"""));  
  14.       }  
  15.       var clsValue = s.attr("class");  
  16.       if (clsValue) {  
  17.         s.attr("class", clsValue.replace("placeholderfriend"""));  
  18.       }  
  19.     }  
  20.   }  
  21.   
  22.   //判断是否支持placeholder  
  23.   function isPlaceholer() {  
  24.     var input = document.createElement('input');  
  25.     return "placeholder" in input;  
  26.   }  
  27.   //不支持的代码  
  28.   if (!isPlaceholer()) {  
  29.     $(function() {  
  30.   
  31.       var form = $(this);  
  32.   
  33.       //遍历所有文本框,添加placeholder模拟事件  
  34.       var elements = form.find("input[type='text'][placeholder]");  
  35.       elements.each(function() {  
  36.         var s = $(this);  
  37.         var pValue = s.attr("placeholder");  
  38.         var sValue = s.val();  
  39.         if (pValue) {  
  40.           if (sValue == '') {  
  41.             s.val(pValue);  
  42.           }  
  43.         }  
  44.       });  
  45.   
  46.       elements.focus(function() {  
  47.         var s = $(this);  
  48.         var pValue = s.attr("placeholder");  
  49.         var sValue = s.val();  
  50.         if (sValue && pValue) {  
  51.           if (sValue == pValue) {  
  52.             s.val('');  
  53.           }  
  54.         }  
  55.       });  
  56.   
  57.       elements.blur(function() {  
  58.         var s = $(this);  
  59.         var pValue = s.attr("placeholder");  
  60.         var sValue = s.val();  
  61.         if (!sValue) {  
  62.           s.val(pValue);  
  63.         }  
  64.       });  
  65.   
  66.       //遍历所有密码框,添加placeholder模拟事件  
  67.       var elementsPass = form.find("input[type='password'][placeholder]");  
  68.       elementsPass.each(function(i) {  
  69.         var s = $(this);  
  70.         var pValue = s.attr("placeholder");  
  71.         var sValue = s.val();  
  72.         if (pValue) {  
  73.           if (sValue == '') {  
  74.             //DOM不支持type的修改,需要复制密码框属性,生成新的DOM  
  75.             var html = this.outerHTML || "";  
  76.             html = html.replace(/\s*type=(['"])?password\1/gi, " type=text placeholderfriend")  
  77.               .replace(/\s*(?:value|on[a-z]+|name)(=(['"])?\S*\1)?/gi, " ")  
  78.               .replace(/\s*placeholderfriend/, " placeholderfriend value='" + pValue  
  79.               + "' " + "onfocus='placeholderfriendfocus(this);' ");  
  80.             var idValue = s.attr("id");  
  81.             if (idValue) {  
  82.               s.attr("id", idValue + "placeholderfriend");  
  83.             }  
  84.             var clsValue = s.attr("class");  
  85.             if (clsValue) {  
  86.               s.attr("class", clsValue + "placeholderfriend");  
  87.             }  
  88.             s.hide();  
  89.             s.after(html);  
  90.           }  
  91.         }  
  92.       });  
  93.   
  94.       elementsPass.blur(function() {  
  95.         var s = $(this);  
  96.         var sValue = s.val();  
  97.         if (sValue == '') {  
  98.           var idValue = s.attr("id");  
  99.           if (idValue) {  
  100.             s.attr("id", idValue + "placeholderfriend");  
  101.           }  
  102.           var clsValue = s.attr("class");  
  103.           if (clsValue) {  
  104.             s.attr("class", clsValue + "placeholderfriend");  
  105.           }  
  106.           s.hide().next().show();  
  107.         }  
  108.       });  
  109.   
  110.     });  
  111.   }  
  112.   window.placeholderfriendfocus = placeholderfriend.focus;  
  113. })(jQuery);  

使用很简单,例子如下:

[html] view plaincopy
  1. <html>  
  2. <head>  
  3. <script src="jquery.js" type="text/javascript"></script>  
  4. <script src="placeholderfriend.js" type="text/javascript"></script>  
  5. </head>  
  6. <body>  
  7. <input placeholder="账号/手机号码" ><br>  
  8. <input placeholder="密码" type="password" >  
  9. </body>  
  10. </html> 
0 0
原创粉丝点击