我怎么能触发一个事件只有当用户是向后而不是向前的选项卡

How can I trigger an event ONLY when the user is tabbing backwards and NOT forward

本文关键字:用户 选项 怎么能 事件 一个      更新时间:2023-09-26

在jQuery中,如何在用户后退而不是前进时触发事件?

$('.menu-item-has-children a').keydown(function (e) {
    if(e.which === 9 && (!e.shiftKey)){
        console.log('Tabin forward');
        $('.sub-menu li:last-child').focusout(function() {
          console.log('check it out');
        $(this).parent().parent().removeClass('hover');
    });
 }
    else if (e.which === 9 && e.shiftKey) {
      $(this).parent().removeClass('hover');
    console.log("back Tab removed");
   }
});

这是我正在使用的代码,但它似乎不工作。第一个if语句在回制表符(shift制表符)时仍然会触发。

问题是,当我按下shift tab到后退tab时,tab事件也会触发,这就是问题所在。

任何帮助或指导将不胜感激。谢谢你。

这是一个JSFiddle,请尝试向后键。注意,当你在子菜单的最后一个LI上后退标签时,它是有效的。因为两个事件都触发....

可能是您将keydown事件绑定到特定元素,而不是将整个文档作为一个整体?

我已经简化了你要做的事情,它作为一个独立的代码片段工作:

$(document).keydown(function (e) {
    if(e.which !== 9) return false;
    if(e.shiftKey){
        console.log('back Tab removed');
    }else{
        console.log("Tabin forward");
    }
});

你想达到什么目标?看起来你只是想在选项卡上打开子菜单,在这种情况下,你可以大大简化你的代码:http://jsfiddle.net/X3cLb/4/