首页 > 解决方案 > 在输入时将光标移动到下一个元素跟踪 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;
            }
        });

我一直在寻找解决方案,但找不到。任何人都可以请帮忙。

标签: javascriptjquery

解决方案


您专门关注下一个元素,而不是让浏览器处理它。因此 tabindex=-1 不起作用。

要使用您在 HTML 中指定的 tabindex,您还必须在 Javascript ( :not([tabindex="-1"])) 中指定它:

 focusable = form.find('input,a,select,button,textarea').filter(':not([tabindex="-1"]):visible');

推荐阅读