ie8中使用placeholder

placeholder 是 html5 中的新属性,考虑到还有很多 ie8 的用户,因此找了一个 ie8 的 placeholder 的补丁,以下:javascript

<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>  

将该段代码放在 </body> 以前便可,须要说明的是,该段代码用到了 jQuery ,因此事先须要引用 jQuery。html

该文不能解决密码类型的 placeholder ,请参考 ie 中支持 password 的 placeholderhtml5

相关文章
相关标签/搜索