rob*_*nal 6 javascript jquery focus
如果焦点是针对click事件或tabstop触发的,如何确定jQuery上的焦点事件?我有这个焦点事件,如果焦点是由一个tabstop触发我将执行一些东西,如果它是一个点击我不会执行它.
这是一个伪代码
$('a').focus(function(){
if(ThisIsTabStop()) {
IWillExecuteThis();
}
});
Run Code Online (Sandbox Code Playgroud)
如果单击某个元素,则会mousedown
在焦点之前触发该事件.您只需设置数据属性,并在焦点事件中进行检查.
试试这个演示: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
}
});
Run Code Online (Sandbox Code Playgroud)