我想检测元素的焦点事件,但前提是该事件是由用户按 Tab 键启动的。例如:
<input type="text" id="foo" />
<input type="text" id="detect" />
If the user is focused on #foo
and presses Tab, I want the event to fire once #detect
becomes focused (or a conditional inside the focus event to be true). Conversely, if the user simply clicks on the #detect
field to focus it, I do not want the event to fire (or I want the conditional inside the focus event call to be false).
我不想使用 keydown 事件#foo
并检查是否按下了 Tab 键,因为我希望该方法独立于任何其他元素。
我查看了以下代码的控制台输出,但没有注意到两种聚焦方法之间有任何真正的区别:
$('#detect').on('focus', function(e){
console.log(e);
});
(fiddle http://jsfiddle.net/LPGLm/)
这可以通过相对简单的方式完成吗?
Update:改变监听器:
$(window).keyup(function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 9 && $('#detect:focus').length) {
alert('I was tabbed!');
}
});
http://jsfiddle.net/LPGLm/7/ http://jsfiddle.net/LPGLm/7/
原来的:我知道您已接受答案,但您可以使用以下方法测试按下的按钮:
$('#detect').on('focus', function(e){
$(window).keyup(function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 9) {
alert('I was tabbed!');
}
});
});
http://jsfiddle.net/LPGLm/1/ http://jsfiddle.net/LPGLm/1/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)