停止TAB键事件传播在chrome后的一个动作

stop tab key event propagation in chrome after an action

本文关键字:一个 TAB 事件 传播 chrome 停止      更新时间:2023-09-26

在Chrome中,我试图拦截tab键按下停止所有正常tab键行为并执行我自己的动作。

如果我只做这些,我可以停止默认行为。但是如果执行一个动作,然后尝试停止默认行为,它阻止焦点转移到下一个输入,但它在当前输入中放置一个表空间。

如果问题仍然不清楚,请查看显示问题的jsFiddle

这是我尝试过的,(注意,下面所有的工作都可以在firefox中完成我需要的工作,而不是在chrome中)

<input type="text" class="next-tab"/>
<input type="text" class="next-tab2"/>
<input type="text" class="next-tab3"/>
$('.next-tab').keydown(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        console.log(code);
        if (code == 0 || code == 9){
            e.preventDefault();
            e.stopPropagation();
        }
    // this only blocks the propagation of the tab keypress event, and works as expected:
    });
$('.next-tab2').keydown(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        console.log(code);
        if (code == 0 || code == 9){
            alert("test");
            e.preventDefault();
            e.stopPropagation();
        }
});
$('.next-tab3').keydown(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        console.log(code);
        if (code == 0 || code == 9){
            e.preventDefault();
            e.stopPropagation();
            alert("test");
            //putting the alert after doesnt help
        }
});

如何阻止表空间被添加到输入?

仅当动作包含alert()confirm()prompt()时出现此问题。这可能与它们重新进入事件循环的方式有关。因此,从代码中删除这些调试语句,它应该可以工作。