html5兼容IE placeholder问题

来源:互联网 发布:java style标签 编辑:程序博客网 时间:2024/06/05 06:45

最近遇到了用html5写的留言板,提示信息用的placeholder,但是在IE6/IE7/IE8/IE9下面不出效果,原因是H5的placeholder属性在IE下没有被支持。

解决方法:

用jQuery写js解决,代码如下

需要先引入jQuery

<script type="text/javascript">
if( !('placeholder' in document.createElement('input')) ){  
  
   $('input[placeholder],textarea[placeholder]').each(function(){   
     var that = $(this),   
     text= that.attr('placeholder');   
     if(that.val()===""){   
       that.val(text).addClass('placeholder');   
     }   
     that.focus(function(){   
       if(that.val()===text){   
         that.val("").removeClass('placeholder');   
       }   
     })   
     .blur(function(){   
       if(that.val()===""){   
         that.val(text).addClass('placeholder');   
       }   
     })   
     .closest('form').submit(function(){   
       if(that.val() === text){   
         that.val('');   
       }   
     });   
   });   
 }  
</script>


记得这段代码需要在dom文档加载完毕后执行,不然不出效果。

0 0
原创粉丝点击