Solution for supporting placeholder attribute in IE

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.