使用 jQuery 焦点和模糊来显示和隐藏消息
我单击在文本框
上设置焦点
,设置焦点后,我将尝试显示一条简单的消息。然后在blur
上该消息消失。
这是我的代码:如果我单击 textbox
它会显示消息,但如果我单击按钮,它不会像我想象的那样设置焦点。
<script>
$(document).ready(function(){
$("#clicker").click(function(){
$("#T1").focus(function(){
$("#myFocus").show();
});
});
$("#T1").blur(function(){
$("#myFocus").hide();
});
});
</script>
<body>
<div id="clicker" style="cursor:pointer; border:1px solid black; width:70px;">
Click here!
</div>
<br /><br />
<input id="T1" name="Textbox" type="text" />
<div id="myFocus" style="display: none;">focused!</div>
I am clicking to set focus
on a textbox
, and once I have set focus I am trying to display a simple message. Then on blur
that message disappears.
Here is my code: If I click on the textbox
it displays the message but if I click the button it doesn't set focus as I thought it would.
<script>
$(document).ready(function(){
$("#clicker").click(function(){
$("#T1").focus(function(){
$("#myFocus").show();
});
});
$("#T1").blur(function(){
$("#myFocus").hide();
});
});
</script>
<body>
<div id="clicker" style="cursor:pointer; border:1px solid black; width:70px;">
Click here!
</div>
<br /><br />
<input id="T1" name="Textbox" type="text" />
<div id="myFocus" style="display: none;">focused!</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要触发焦点事件,而不是定义它。试试这个:
You need to trigger the focus event, instead of defining it. Try this instead:
问题在于:
您应该使用
focus()
触发事件,而不是使用focus(function(){...}
附加回调固定代码:< /强>
The problem is here:
You should trigger the event with
focus()
not attach a callback withfocus(function(){...}
Fixed code: