input表单文本内容鼠标点击消失,鼠标移走恢复

第一种,最简单的办法

就是直接在input表单里加上 onfocus="this.value=''" onblur="this.value=''"即<input type="text" value="1111" onfocus="this.value=''" onblur="this.value='1111'"/>

第二种,用jquery实现:(网上找的例子)

js:

代码
$(function() {
//取出有clear类的input域
//
(注: "clear once" 是两个class clear 和 once)
$('#testform input.clear').each(function(){
//使用data方法存储数据
    $(this).data( "txt", $.trim($(this).val()) );
})
.focus(
function(){
// 获得焦点时判断域内的值是否和默认值相同,如果相同则清空
    if ( $.trim($(this).val()) === $(this).data("txt") ) {
    $(
this).val("");
  }
})
.blur(
function(){
// 为有class clear的域添加blur时间来恢复默认值
//
 但如果class是once则忽略
    if ( $.trim($(this).val()) === "" && !$(this).hasClass("once") ) {
//Restore saved data
    $(this).val( $(this).data("txt") );
    }
  });
});

Dom:

<input type="text" class="clear" value="Always cleared" />
<input type="text" class="clear once" value="Cleared only once" />
<input type="text" value="Normal text" />
原文地址:https://www.cnblogs.com/lch880/p/1684222.html