How to determine the focus event on jQuery if the focus is fired on click event or tabstop? I have this focus event that if the focus is fired by a tabstop I will execute something and if it is a click I will not execute it.

如果焦点在单击事件或tabstop上触发,如何确定jQuery上的焦点事件?我有一个焦点事件,如果焦点被tabstop触发,我将执行一些东西,如果是点击,我将不会执行它。

Here's a pseudo code

这是一个伪代码

$('a').focus(function(){
    if(ThisIsTabStop()) {
         IWillExecuteThis();
    }
});

1 个解决方案

#1


7

If an element is clicked, the mousedown event fires before focus. You just need to set a data attribute, and check it in the focus event.

如果单击了一个元素,那么mousedown事件将在focus之前触发。您只需要设置一个数据属性,并在焦点事件中检查它。

Try this demo: http://jsfiddle.net/KcGcF/1/

试试这个演示:http://jsfiddle.net/KcGcF/1/

$('a').mousedown(function(e){
    var $this = $(this);

    // Only set the data attribute if it's not already focused, as the
    // focus event wouldn't fire afterwards, leaving the flag set.
    if(!$this.is(':focus')){
        $this.data('mdown',true);
    }
});

$('a').focus(function(e){
    var $this = $(this);
    var mdown = $this.data('mdown');

    // Remove the flag so we don't have it set next time if the user
    // uses the tab key to come back.
    $this.removeData('mdown');

    // Check if the mousedown event fired before the focus
    if(mdown){
        // Click
    } else {
        // Tab
    }
});

更多相关文章

  1. jQuery 事件绑定方法(bind hover toggle live.... )、删除事件方法
  2. 使用JQuery显示和隐藏不同div的onClick事件
  3. jQuery中的bind绑定事件与文本框改变事件的临时解决方法
  4. 在asp.net中捕获href事件
  5. fullcalendar.js - 在按钮点击时删除事件
  6. 使用jquery 1.11.3.min.js时,jquery on click事件未触发。
  7. bootstrap,模态对话框,shown.bs.modal事件不会触发
  8. 更改html隐藏字段的事件
  9. JQuery 总结(3) jQuery 各种事件

随机推荐

  1. 深入SQLite多线程的使用总结详解
  2. XML入门的常见问题(四)
  3. java中枚举的详细使用介绍
  4. XML入门的常见问题(三)
  5. J2ME Mobile 3D入门教程系列文章之一
  6. XML入门的常见问题(二)
  7. XML入门的常见问题(一)
  8. php输出xml格式字符串
  9. go语言是脚本语言吗
  10. go是动态语言还是静态语言