检测由标签键启动的焦点

检测由标签键启动的焦点

本文介绍了检测由标签键启动的焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想检测一个元素的焦点事件,但只有当用户按Tab键启动它时。例如:I want to detect the focus event of an element, but only if it was initiated by the user pressing the tab key. For example:<input type="text" id="foo" /><input type="text" id="detect" />如果用户专注于 #foo 并按,我想要事件一旦 #detect 变得专注(或焦点事件中的条件为true)即可​​触发。相反,如果用户只需点击 #detect 字段来对焦,我不希望事件触发(或者我想要焦点事件调用中的条件为假)。If the user is focused on #foo and presses , 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).我不想使用 #foo 的keydown事件,并检查tab键是否按下,因为我想要的方法是独立于任何其他元素。I don't want to use the keydown event of #foo and check if the tab key was pressed, as I want the approach to be independent of any other element.我看过以下代码的控制台输出,但是没有注意到两者之间的真正差异聚焦方法:I looked through the console output of the following code, but couldn't notice any real differences between the two methods of focusing:$('#detect').on('focus', function(e){ console.log(e);});(小提琴)(fiddle)这是否可以以相对简单的方式完成?Is this possible to accomplish in a relatively simple way?推荐答案我知道您已经接受了一个答案,但您可以使用以下内容测试按下的按钮:I know you have accepted an answer but you could test the button pressed using the following:$('#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/ 修改:更改收听者:$(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/ 这篇关于检测由标签键启动的焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 06:27