【发布时间】:2011-08-31 10:33:00
【问题描述】:
我有 jQuery 代码,它创建了一个可聚焦元素数组,并为左右箭头绑定了 .keydown 以通过它们进行制表符。在 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();
}
});
});
});
【问题讨论】:
标签: jquery firefox event-handling preventdefault