$(document).ready(function(){
// 兼容IE9下的placeholder
function placeholderSupport() {
return 'placeholder' in document.createElement('input');
}
if(!placeholderSupport()){ // 判断浏览器是否支持 placeholder
$("[placeholder]").each(function(){
var _this=$(this);
if(_this[0].tagName=='TEXTAREA'){
_this.parent().css("position","relative").append('<span class="placeholder" data-type="placeholder" style="position: absolute;top:26px;left:2px;">'+_this.attr("placeholder")+'</span>');
}else if(_this[0].tagName=='SELECT'){

}else{
_this.parent().append('<span class="placeholder" data-type="placeholder" style="position: absolute;bottom:7px;left:2px;">'+_this.attr("placeholder")+'</span>');
}

if(_this.val() != ""){
_this.parent().find("span.placeholder").hide();
}else{
_this.parent().find("span.placeholder").show();
}
}).on("focus", function(){
$(this).parent().find("span.placeholder").hide();
}).on("blur", function(){
var _this = $(this);
if(_this.val() != ""){
_this.parent().find("span.placeholder").hide();
}else{
_this.parent().find("span.placeholder").show();
}
});
// 点击表示placeholder的标签相当于触发input
$("span.placeholder").on("click", function(){
$(this).hide();
$(this).siblings("[placeholder]").trigger("click");
$(this).siblings("[placeholder]").trigger("focus");
});
}
})

posted on 2018-07-18 17:09  努力才会进步  阅读(148)  评论(0编辑  收藏  举报