1.css
var funPlaceholder = function(element) {
var placeholder = '';
if (element && !("placeholder" in document.createElement("input"))
&& (placeholder = element.getAttribute("placeholder"))) {
element.onfocus = function() {
if (this.value === placeholder) {
this.value = "";
}
this.style.color = '#000';
};
element.onblur = function() {
if (this.value === "") {
this.value = placeholder;
this.style.color = '#999';
} else {
this.style.color = '#000';
}
};this
// 樣式初始化
if (element.value === "") {
element.value = placeholder;
element.style.color = '#999';
}
}
};spa
2.element
var IePlaceholder=function(){
var doc = window.document, input = doc.createElement('input');
if( typeof input['placeholder'] == 'undefined' ) // 若是不支持placeholder屬性
{
$('input').each(function( ele )
{
var me = $(this);
var ph = me.attr('placeholder');
if( ph && !me.val() )
{
me.val(ph).css('color', '#aaa').css('line-height', me.css('height'));
}
me.on('focus', function()
{
if( me.val() === ph)
{
me.val(null).css('color', '');
}
}).on('blur', function()
{
if( !me.val() )
{
me.val(ph).css('color', '#aaa').css('line-height', me.css('height'));
}
});
});
}
}get