フォーカス可能な要素の配列を作成し、左矢印と右矢印の .keydown をバインドしてそれらをタブで移動する jQuery コードがあります。Chrome、IE、および Safari ではpreventDefault()
、return false で開始または終了する (これは必要がないため、技術的には使用したくないstopPropagation()
) 矢印のデフォルト イベントを防止しますが、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();
}
});
});
});