就一个函数,用的时候可以复制一下~
$("input[type='text']").on("blur focus",function(){
//1 获得默认值
var dv = $(this).attr("defaultValue");
//2 判断是否获得焦点
if( $(this).is(":focus") ){
//2.1 获得焦点,如果是默认值 ,清空value值 ,this 当前执行对象,是dom对象
if( $(this).val() == dv){
$(this).val("");
$(this).css("color","#000");
}
} else {
//2.2 失去焦点,如果内容为空,设置默认值 , 或 $(this).val().length == 0
if( $(this).val() == "" ){
$(this).val(dv);
$(this).css("color","#999");
}
}
});
});