IE8兼容placeholder

placeholder属性树html5提出的新属性,作用是为input框或textarea框添加初始提示内容。当控件获取焦点输入时,清空提示内容。但IE8及其以下的版本并不支持该属性的显示。

下面做如下处理:

$(function(){
	 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('');    
		        }    
		      });    
		    });  
	 }
});
第一个if条件是判断placeholder是否存在于解析的input框中。

closest方法的作用是:从当前元素开始向上查找,直到找到匹配的元素为止。

上诉closest方式计算查找form元素,然后在执行提交的form表单的时候执行sumit的方法。


上面添加了一个palceholder样式,其样式内容为:

.placeholder{
	opacity:0.5;
	filter:alpha(opacity=50);
}

opcaity属性是用来定义透明度的,其值在0-1.0之间。但IE8及其以下的版本并不支持。

在IE8上需用filter:alpha(opacity=?)来过滤,opacity的值在0-100间。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值