确定焦点事件:单击或tabstop
16lz
2021-01-22
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
}
});
更多相关文章
- jQuery 事件绑定方法(bind hover toggle live.... )、删除事件方法
- 使用JQuery显示和隐藏不同div的onClick事件
- jQuery中的bind绑定事件与文本框改变事件的临时解决方法
- 在asp.net中捕获href事件
- fullcalendar.js - 在按钮点击时删除事件
- 使用jquery 1.11.3.min.js时,jquery on click事件未触发。
- bootstrap,模态对话框,shown.bs.modal事件不会触发
- 更改html隐藏字段的事件
- JQuery 总结(3) jQuery 各种事件