In HTML5, the text box, that is, input, type is text, or password, adds a property placeholder, that is, a placeholder. When the input is complete, the placeholder disappears.
This attribute is very useful, because HTML5 is required to have this attribute. However, in IE, this effect is not achieved, so we can achieve the compatibility of ie9.
I wrote a jquery plug-in to enable IE to achieve this effect.
The Code is as follows:
jQuery.fn.placeholder = function(){
var i = document.createElement(‘input‘),
placeholdersupport = ‘placeholder‘ in i;
if(!placeholdersupport){
var inputs = jQuery(this);
inputs.each(function(){
var input = jQuery(this),
text = input.attr(‘placeholder‘),
pdl = 0,
height = input.outerHeight(),
width = input.outerWidth(),
placeholder = jQuery(‘<span class="phTips">‘+text+‘</span>‘);
try{
pdl = input.css(‘padding-left‘).match(/\d*/i)[0] * 1;
}catch(e){
pdl = 5;
}
placeholder.css({‘margin-left‘: -(width-pdl),‘height‘:height,‘line-height‘:height+"px",‘position‘:‘absolute‘, ‘color‘: "#6C7C88", ‘font-size‘ : "12px"});
placeholder.click(function(){
input.focus();
});
if(input.val() != ""){
placeholder.css({display:‘none‘});
}else{
placeholder.css({display:‘inline‘});
}
placeholder.insertAfter(input);
input.keyup(function(e){
if(jQuery(this).val() != ""){
placeholder.css({display:‘none‘});
}else{
placeholder.css({display:‘inline‘});
}
});
});
}
return this;
};代码不复杂简单易懂~直接写入JS文件引用便可调用,不与firefox,google的属性冲突
Introduce the jquery library and then introduce this plug-in. The call is as follows:
$(function (){
jQuery(‘input[placeholder]‘).placeholder();
}
);
样式也可以自由修改如下:
placeholder.css({
‘margin-left‘: -(width-pdl),
‘height‘:height,
‘line-height‘:height+"px",
‘position‘:‘absolute‘,
‘color‘: "#cecfc9",
‘font-size‘ : "12px"});
如果存在什么bug请大家回复以便及时修正~
From Weizhi note (wiz)
Solution for supporting placeholder attribute in IE