我正在使用以下代码移动到下一个元素。除了 tabindex=-1 之外,代码工作正常。它不会跳过 tabindex 设置为 -1 的元素。
$('body').on('keydown', 'input, select', function(e) {
if (e.key === "Enter") {
var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
focusable = form.find('input,a,select,button,textarea').filter(':visible');
next = focusable.eq(focusable.index(this)+1);
if (next.length) {
next.focus();
} else {
form.submit();
}
return false;
}
});
我一直在寻找解决方案,但找不到。 任何人都可以帮忙吗?
最佳答案
您专门关注下一个元素,不让浏览器处理它。因此 tabindex=-1 不起作用。
要使用您在 HTML 中指定的 tabindex,您还必须在 Javascript 中指定它( :not([tabindex="-1"])
):
focusable = form.find('input,a,select,button,textarea').filter(':not([tabindex="-1"]):visible');
关于javascript - 将光标移动到 Enter 上的下一个元素,跟踪 tabindex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61040520/