阻止Firefox中jQuery键盘按下的默认事件

| 我有jQuery代码,它使可聚焦元素的数组组成并绑定.keydown左右箭头以通过它们进行制表。在Chrome,IE和Safari中,以“ 0”开头或以return false结尾(技术上我不想使用,因为我不需要使用“ 1”)结束了箭头的默认事件,但在Firefox中却没有。 如何防止Firefox中的默认操作? 这是可以正常工作的代码,但在Firefox中,除了我的回调外,还会触发默认事件。
$(function () {
    var focusables = $(\":focusable\");
    focusables.eq(0).focus();
    focusables.eq(0).select();
    focusables.each(function () {
        $(this).keydown(function (e) {
            if (e.which == \'37\') { // left-arrow
                e.preventDefault();
                var current = focusables.index(this),
                    next = focusables.eq(current - 1).length ? focusables.eq(current - 1) : focusables.eq(0);
                next.focus();
                next.select();
            }
            if (e.which == \'39\') { // right-arrow
                e.preventDefault();
                var current = focusables.index(this),
                    next = focusables.eq(current + 1).length ? focusables.eq(current + 1) : focusables.eq(0);
                next.focus();
                next.select();
            }
        });
    });
});
    
已邀请:
keypress事件是需要取消的事件,但是在这种情况下,Firefox会忽略preventDefault()。因此,解决方案是模糊当前下拉菜单,让按键事件在文档上触发,并通过超时将焦点设置为新的下拉菜单。
var focusables = $(\":focusable\");
focusables.eq(0).focus().select();
focusables.each(function () {
    $(this).keydown(function (e) {
        if (e.which == \'37\') { // left-arrow
            e.preventDefault();
            var current = focusables.index(this),
                next = focusables.eq(current - 1).length ? focusables.eq(current - 1) : focusables.eq(0);
            this.blur();
            setTimeout(function() { next.focus().select(); }, 50);
        }
        if (e.which == \'39\') { // right-arrow
            e.preventDefault();
            var current = focusables.index(this),
                next = focusables.eq(current + 1).length ? focusables.eq(current + 1) : focusables.eq(0);
            this.blur();
            setTimeout(function() { next.focus().select(); }, 50);
        }
    });
});
演示位于http://jsfiddle.net/roberkules/3vA53/     
你有尝试过吗?
$(selector).click(function(event) {
   event.preventDefault();
});
    

要回复问题请先登录注册