Chrome not allowing tab prevendefault

爱⌒轻易说出口 提交于 2019-12-20 05:58:50

问题


I have a form on which user can tab and jump to different elements. i was to stop the tab when it reaches a special anchor tag.

this is the code which work in firefox

    $('.next-tab').keypress(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        console.log(code);
        if (code == 0 || code == 9){
            console.log("keypress")
            e.preventDefault();
            e.stopPropagation();
        }
    });

but this code does not work in chrome, i dont know why, it does not even enter the keypress method. so i used this code for chrome

$('.next-tab').blur(function(e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            console.log(code);
            if (code == 0 || code == 9){
                console.log("blur")
                e.preventDefault();
                e.stopPropagation();
            }
        });

it enters the blur method pass the condition but dont do any thing, and user can easily move to the next element.


回答1:


For chrome support, looks like you need to use keydown event:

http://jsfiddle.net/qD2rk/

$('.next-tab').keydown(function (e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    console.log(code);
    if (code == 0 || code == 9) {
        console.log("keydown")
        e.preventDefault();
        e.stopPropagation();
    }
});


来源:https://stackoverflow.com/questions/17073092/chrome-not-allowing-tab-prevendefault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!