Javascript 确定焦点事件:单击或tabstop
如果焦点是在单击事件或tabstop时触发的,如何确定jQuery上的焦点事件?我有一个焦点事件,如果焦点是由一个tabstop触发的,我将执行一些东西,如果是单击,我将不执行它 这是一个伪代码Javascript 确定焦点事件:单击或tabstop,javascript,jquery,focus,Javascript,Jquery,Focus,如果焦点是在单击事件或tabstop时触发的,如何确定jQuery上的焦点事件?我有一个焦点事件,如果焦点是由一个tabstop触发的,我将执行一些东西,如果是单击,我将不执行它 这是一个伪代码 $('a').focus(function(){ if(ThisIsTabStop()) { IWillExecuteThis(); } }); 如果单击某个元素,则会在焦点之前触发mousedown事件。您只需要设置一个数据属性,并在焦点事件中检查它 尝试此演示:
$('a').focus(function(){
if(ThisIsTabStop()) {
IWillExecuteThis();
}
});
如果单击某个元素,则会在焦点之前触发
mousedown
事件。您只需要设置一个数据属性,并在焦点事件中检查它
尝试此演示:
我没有一个具体的答案,但一种方法可能是使用
keypress()
事件来存储上次按下Tab键的时间,如果是很短的一段时间之前,您可以相当确信这是一个Tab停止。此外,要检测单击,请绑定到单击事件。我不确定哪个先执行,但可能在单击处理程序中的元素上设置一个数据项并在焦点处理程序中检查该数据项是另一种方法。focus
事件总是在click
之前触发,但mousedown
在focus
之前触发。
$('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
}
});