ie9 placeholder兼容

强颜欢笑 提交于 2019-11-28 06:12:10

 

.phcolor{ color:#999;}//css样式

function isPlaceholer(){
var input = document.createElement("input");

return "placeholder" in input;

};
function placeholder(input){

var text = input.attr('placeholder'),
defaultValue = input.defaultValue;

if(!defaultValue){

input.val(text).addClass("phcolor");
}

input.focus(function(){

if(input.val() == text){

$(this).val("");
}
});


input.blur(function(){

if(input.val() == ""){

$(this).val(text).addClass("phcolor");
}
});

//输入的字符不为灰色
input.keydown(function(){

$(this).removeClass("phcolor");
});
};

//当浏览器不支持placeholder属性时,调用placeholder函数
if(!isPlaceholer()){

$('input').each(function(){

text = $(this).attr("placeholder");

if($(this).attr("type") == "text"){

placeholder($(this));
}
});
}//结束

 

也有可用jquery-placeholder.js插件解决。但插件字体颜色不是灰色。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!