ie8中使用placeholder

时间:2023-03-09 07:53:56
ie8中使用placeholder

placeholder 是 html5 中的新属性,考虑到还有不少 ie8 的用户,所以找了一个 ie8 的 placeholder 的补丁,如下:

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

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