简体   繁体   English

辅助功能 - 如何在元素上设置自定义选项卡焦点顺序

[英]Accessibility - How to set custom tab focus order on elements

My use case is a traditional form page.我的用例是一个传统的表单页面。 My page contains a few buttons on the top right followed by form input fields.我的页面在右上角包含几个按钮,然后是表单输入字段。

When using mouse-less navigation and pressing tab, the standard behavior is exhibited where first focus is set on the browser tab and the browser URL and then into my actual web page in the order of elements (buttons on the top right into the form fields).使用无鼠标导航和按选项卡时,会显示标准行为,其中首先将焦点设置在浏览器选项卡和浏览器 URL 上,然后按元素顺序进入我的实际 web 页面(右上角的按钮进入表单字段)。

However, it seems better accessibility would have pressing tab on the last form field jump straight to the "Save" button on the top right of the page instead of having to loop through the browser tab and then browser URL again.然而,似乎更好的可访问性将在最后一个表单字段上按下选项卡直接跳转到页面右上角的“保存”按钮,而不必循环浏览浏览器选项卡,然后再次浏览器 URL。 So essentially, I want to "skip" the traditional order of focus on elements after pressing tab on a specific element (the last focusable element on my page which is the last form field) so that it goes to another specific element I want.所以本质上,我想“跳过”在特定元素(我的页面上的最后一个可聚焦元素是最后一个表单字段)上按 Tab 键后关注元素的传统顺序,以便它转到我想要的另一个特定元素。 Is there a way to set this behavior using HTML/CSS/JS?有没有办法使用 HTML/CSS/JS 设置这种行为?

There are two ways to handle this.有两种方法可以处理这个问题。

The first is with tabindex .第一个是tabindex You can control the order of all elements but you have to be very careful.您可以控制所有元素的顺序,但必须非常小心。 It can make the tab order more confusing.它可以使标签顺序更加混乱。 The bigger the tabindex, the higher precedence the element has in the tab order. tabindex 越大,元素在 tab 顺序中的优先级越高。 So you could put tabindex='2' on all your form elements and tabindex='1' on your buttons and leave the rest of the elements on the page without a tabindex.因此,您可以将tabindex='2'放在所有表单元素上,将tabindex='1'放在按钮上,并在没有tabindex 的页面上留下元素的 rest。 (Multiple elements can have the same tabindex value. The browser will tab through them in their respective DOM order.) (多个元素可以具有相同的 tabindex 值。浏览器将按各自的 DOM 顺序在它们之间切换。)

The second way to handle this is with the DOM order itself and then use CSS to move the elements where you want them.处理此问题的第二种方法是使用 DOM 顺序本身,然后使用 CSS 将元素移动到您想要的位置。 So the DOM could have the form elements first and then the buttons second.所以 DOM 可以先有表单元素,然后再有按钮。 You can then use CSS to put the buttons in the upper right.然后,您可以使用 CSS 将按钮放在右上角。 This would be the preferred method because it's not as "dangerous" as tabindex.这将是首选方法,因为它不像 tabindex 那样“危险”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM