placeholder兼容问题

  1. (function($) {  
  2.   var placeholderfriend = {  
  3.     focus: function(s) {  
  4.       s = $(s).hide().prev().show().focus();  
  5.       var idValue = s.attr("id");  
  6.       if (idValue) {  
  7.         s.attr("id", idValue.replace("placeholderfriend", ""));  
  8.       }  
  9.       var clsValue = s.attr("class");  
  10.       if (clsValue) {  
  11.         s.attr("class", clsValue.replace("placeholderfriend", ""));  
  12.       }  
  13.     }  
  14.   }  
  15.   
  16.   //判断是否支持placeholder  
  17.   function isPlaceholer() {  
  18.     var input = document.createElement('input');  
  19.     return "placeholder" in input;  
  20.   }  
  21.   //不支持的代码  
  22.   if (!isPlaceholer()) {  
  23.     $(function() {  
  24.   
  25.       var form = $(this);  
  26.       var elements = form.find("input[type='text'][placeholder]");  
  27.       elements.each(function() {  
  28.         var s = $(this);  
  29.         var pValue = s.attr("placeholder");  
  30.         var sValue = s.val();  
  31.         if (pValue) {  
  32.           if (sValue == '') {  
  33.             s.val(pValue);  
  34.           }  
  35.         }  
  36.       });  
  37.   
  38.       elements.focus(function() {  
  39.         var s = $(this);  
  40.         var pValue = s.attr("placeholder");  
  41.         var sValue = s.val();  
  42.         if (sValue && pValue) {  
  43.           if (sValue == pValue) {  
  44.             s.val('');  
  45.           }  
  46.         }  
  47.       });  
  48.   
  49.       elements.blur(function() {  
  50.         var s = $(this);  
  51.         var pValue = s.attr("placeholder");  
  52.         var sValue = s.val();  
  53.         if (!sValue) {  
  54.           s.val(pValue);  
  55.         }  
  56.       });  
  57.   
  58.       var elementsPass = form.find("input[type='password'][placeholder]");  
  59.       elementsPass.each(function(i) {  
  60.         var s = $(this);  
  61.         var pValue = s.attr("placeholder");  
  62.         var sValue = s.val();  
  63.         if (pValue) {  
  64.           if (sValue == '') {  
  65.             var html = this.outerHTML || "";  
  66.             html = html.replace(/\s*type=(['"])?password\1/gi, " type=text placeholderfriend").replace(/\s*(?:value|on[a-z]+|name)(=(['"])?\S*\1)?/gi, " ").replace(/\s*placeholderfriend/, " placeholderfriend value='" + pValue + "' " + "onfocus='placeholderfriendfocus(this);' ");  
  67.             var idValue = s.attr("id");  
  68.             if (idValue) {  
  69.               s.attr("id", idValue + "placeholderfriend");  
  70.             }  
  71.             var clsValue = s.attr("class");  
  72.             if (clsValue) {  
  73.               s.attr("class", clsValue + "placeholderfriend");  
  74.             }  
  75.             s.hide();  
  76.             s.after(html);  
  77.           }  
  78.         }  
  79.       });  
  80.   
  81.       elementsPass.blur(function() {  
  82.         var s = $(this);  
  83.         var sValue = s.val();  
  84.         if (sValue == '') {  
  85.           var idValue = s.attr("id");  
  86.           if (idValue) {  
  87.             s.attr("id", idValue + "placeholderfriend");  
  88.           }  
  89.           var clsValue = s.attr("class");  
  90.           if (clsValue) {  
  91.             s.attr("class", clsValue + "placeholderfriend");  
  92.           }  
  93.           s.hide().next().show();  
  94.         }  
  95.       });  
  96.   
  97.     });  
  98.   }  
  99.   window.placeholderfriendfocus = placeholderfriend.focus;  
  100. })(jQuery);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值