简体   繁体   English

jQuery焦点并单击在元素上同时运行

[英]JQuery focus and click running on same time on element

I have a button which has 2 different behaviors 我有一个具有2种不同行为的按钮

  1. While navigating with keyboard with tab key, when the button gets blur is should move focus to button 1 使用Tab键在键盘上导航时,当按钮模糊时,应将焦点移至按钮1
  2. When we hit enter key, then it should move focus to button 2 当我们按下回车键时,它应该将焦点移至按钮2

Case 1 is working fine, but when we hit enter key the focus moves to Button 2 and then moving to button 1, Here I found blur also calling when we hit the enter button so it calling the concern function, is there any way to achieve my goal? 情况1运行正常,但是当我们按Enter键时,焦点移至Button 2,然后移至Button 1,在这里我发现当我们按Enter键时也调用了模糊,因此它调用了关注功能,有什么方法可以实现我的目标?

$('#btn').on('focus', function(){
    }).on('blur', function(){
         setTimeout(function() {
            $('.span-hlo')[0].focus(); 
        },250);
});
$('#btn').on('click', function(){
setTimeout(function() {
    $('.span-welcome')[0].focus(); 
  },250);
});

Fiddle example 小提琴的例子

Update your jquery Like this 像这样更新你的jquery

var lastClick = null;
$('#btn').mousedown(function(e) {
  lastClick = e.target;
}).focus(function(e){
    if (e.target == lastClick) {
      $('.span-welcome')[0].focus(); 
    } else {
      $('.span-hlo')[0].focus();    
    }
    lastClick = null;

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM