Jquery焦点与失去焦点

本文介绍了一种改进的JavaScript事件处理方法,通过使用jQuery库,实现了输入框获得焦点时执行特定操作,并在失去焦点时触发警报。适用于需要在用户交互时执行特定逻辑的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

$( function (){
$( "#input" ).focus( function (){ //#input换成你的input的ID
//这里写获得焦点之后运行的代码。
}).blur(function(){
    //$(".scroll-question").css("display","none");
    });
})

})



live要1.4才支持,以下ie,firefox都没问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="/js/jquery-1.4.4.js"></script>
<script>
$(document).ready(function()
{
 $("#nameBefore").dblclick(function()
 {
  $("#nameBefore").replaceWith("<div id='nameIn'><input type='text' name='nameInVal' id='nameInVal' value='In' /></div>");
 }
 );
 //失去焦点以后
 $("#nameInVal").live("blur",function()
 {
  alert('nameInVal已失去焦点');
 }
 );
}    
);
</script>
</head>
<body>
<form name="testfm" method="post" action="save.php">        
<table>
<tr><td>1</td><td><div id='nameBefore'>Before</div></td></tr>
</table>
</form>    
</body>
</html> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值