使用jQuery焦点和模糊来显示和隐藏消息 [英] Using jQuery focus and blur to show and hide a message

查看:147
本文介绍了使用jQuery焦点和模糊来显示和隐藏消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我点击在文本框上设置焦点,一旦我设置焦点,我正在尝试显示一个简单的消息。然后在 blur 那个消息消失。

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.

这里是我的代码:如果我点击 textbox 它显示消息,但如果我点击按钮,它不会设置焦点,因为我认为它会。

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>


推荐答案

您需要触发焦点事件,它。试试这个:

You need to trigger the focus event, instead of defining it. Try this instead:

<script>
$(function() { // Shorthand for $(document).ready(function() {
      $('#clicker').click(function() {
            $('#T1').focus(); // Trigger focus
      });

      $('#T1').focus(function() { // Define focus handler
            $('#myFocus').show();
      }).blur(function() {
            $('#myFocus').hide();
      });
});
</script>

这篇关于使用jQuery焦点和模糊来显示和隐藏消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆