javascript - 在输入时将光标移动到下一个元素跟踪 tabindex
问题描述
我正在使用以下代码移动到下一个元素。代码工作正常,除了 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 - 如何在电子中使用 import 或 require
- javascript - React:尝试更新嵌套数组中的文本
- javascript - 如何将行跨度属性添加到动态创建的表格单元格
- mongodb - MongoDB Aggregation Pipeline:获取最后第 N 条记录
- keras - 自定义 ImageDataGenerator keras
- node.js - Nestjs中如何实现多个passport jwt认证策略
- node.js - Express.js Find URL of client consuming REST API
- vhdl - Xilinx ISE:找到运算符“+”的“0”定义,无法确定“+”的精确重载匹配定义
- kotlin - 将字符添加到列表
在科特林 - amazon-web-services - 413 请求实体太大 AWS Elastic Beanstack