在操作后停止Chrome中的Tab键事件传播

时间:2014-05-23 20:18:23

标签: javascript tabs textarea keycode

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

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

如果问题仍然不明确,请参阅this jsFiddle showing the issue

以下是我尝试过的内容(请注意以下所有内容都是为了在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

        }
});

如何阻止将标签空间添加到输入中?

1 个答案:

答案 0 :(得分:1)

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

相关问题