JQuery 输入文本框 简单事件

来源:互联网 发布:手机屏幕镜像软件 编辑:程序博客网 时间:2024/06/06 00:20

就一个函数,用的时候可以复制一下~



$("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");
}
}


});
});
原创粉丝点击