一个景点的给input域一个默认值,然后在聚焦的时候清空它 jquery方法

  HTML部分:

< form id ="testform" >
 
   
< input type ="text" class ="clear" value ="Always cleared" />
< input type ="text" class ="clear once" value ="Cleared only once" />
< input type ="text" value ="Normal text" />
</ form >

  JavaSript部分:

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

转载于:https://www.cnblogs.com/flysnow-z/archive/2009/12/07/1618449.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值