jQuery 捕获“shift+tab”按键事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6421936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:50:44  来源:igfitidea点击:

Capture "shift+tab" keypress event

jquery

提问by Hitesh Gupta

I have a case where I want to capture the simultaneous key press event of "shift+tab" key using jquery. Actually as you all know it is used to move tab in backward direction but for me in one scenario tha tab was not working in any direction i.e. neither forward nor backward. so I found a jquery function for moving tab in forward direction as follows:-

我有一个案例,我想使用 jquery 捕获“shift+tab”键的同时按键事件。实际上,众所周知,它用于向后移动选项卡,但对我而言,在一种情况下,选项卡不能在任何方向上工作,即既不向前也不向后。所以我找到了一个用于向前移动选项卡的 jquery 函数,如下所示:-

$(':input').live('keydown', function(e) {    
    var keyCode = e.keyCode || e.which;     
    if (keyCode == 9) {    
        tindex = parseInt($(this).attr("tabindex")) + 1;
        if($(":input[tabindex='" + tindex + "']"))
        {
            $(":input[tabindex='" + tindex + "']").focus();
        }
    }
});

Now I want to move tah tab in backward direction as well. Can somebody guide me how can i achieve this???

现在我也想向后移动 tah 标签。有人可以指导我如何实现这一目标吗???

回答by James Allardice

You can use e.shiftKeyto check whether the shift key was held when the event was triggered.

您可以使用e.shiftKey来检查事件触发时是否按住 shift 键。

If you add an ifstatement to your event handler, checking whether the shift key was held, you can perform different actions:

如果您if向事件处理程序添加一条语句,检查是否按住了 shift 键,您可以执行不同的操作:

if(keyCode == 9) {
    if(e.shiftKey) {
       //Focus previous input
    }
    else {
       //Focus next input
    }
}