我有一段jQuery代码,用于创建一个可聚焦元素的数组,并绑定.keydown事件以通过按左右箭头键来进行遍历。在Chrome、IE和Safari中,以preventDefault()开头或以return false结尾(我不想使用stopPropagation(),因为我没有这个需要)可以防止箭头键的默认事件,但是在Firefox中却无效。
请问如何在Firefox中也能防止默认行为?
以下是代码,除了在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();
}
});
});
});