HTML tabindex 属性是什么?
HTML 中的 tabindex
属性有何用途?
What is the tabindex
attribute used for in HTML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
HTML 中的 tabindex
属性有何用途?
What is the tabindex
attribute used for in HTML?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
tabindex
是 < a href="http://dev.w3.org/html5/markup/global-attributes.html" rel="noreferrer">全局属性负责两件事:在我看来,第二件事比第一件事更重要。默认情况下可聚焦的元素非常少(例如 和表单控件)。开发人员经常在不可聚焦的元素(
tabindex="0"
:此外,如果您不希望通过Tab 键,然后使用
tabindex="-1"
。例如,下面的链接在使用 Tab 键遍历时不会获得焦点。tabindex
is a global attribute responsible for two things:In my mind the second thing is even more important than the first one. There are very few elements that are focusable by default (e.g. <a> and form controls). Developers very often add some JavaScript event handlers (like 'onclick') on not focusable elements (<div>, <span> and so on), and the way to make your interface be responsive not only to mouse events but also to keyboard events (e.g. 'onkeypress') is to make such elements focusable. Lastly, if you don't want to set the order but just make your element focusable use
tabindex="0"
on all such elements:Also, if you don't want it to be focusable via the tab key then use
tabindex="-1"
. For example, the below link will not be focused while using tab keys to traverse.当用户按下选项卡按钮时,用户将按照 1、2 和 3 的顺序浏览表单,如下例所示。
例如:
When the user presses the tab button the user will be taken through the form in the order 1, 2, and 3 as indicated in the example below.
For example:
tabindex 用于定义用户遵循的顺序当他们使用 Tab 键浏览页面时。默认情况下,自然跳位顺序将与标记中的源顺序匹配。
tabindex
从 0 或任何正整数开始,并向上递增。通常会避免使用值 0,因为在旧版本的 Mozilla 和 IE 中,tabindex 将从 1 开始,移至 2,只有在 2 之后才会变为 0,然后变为 3。的最大整数值tabindex
是32767
。如果元素具有相同的tabindex
,则 tabindex 将与标记中的源顺序匹配。负值将从选项卡索引中删除该元素,因此它永远不会获得焦点。如果为某个元素分配了
tabindex
为-1
,它将删除该元素,并且它永远不会成为焦点,但可以使用element 以编程方式将焦点赋予该元素。焦点()。
如果您指定的
tabindex
属性没有值或为空值,它将被忽略。如果在具有
tabindex
的元素上设置了disabled
属性,则该元素将被忽略。如果
tabindex
设置在页面内的任何位置,无论它相对于其余代码的位置(可能在页脚、内容区域等任何位置),如果定义了 < code>tabindex 那么 Tab 键顺序将从显式指定大于 0 的最低tabindex
值的元素开始。然后它将循环遍历定义的元素,并且仅在显式之后tabindex
元素已经被 Tab 键切换过,它会返回到文档的开头并遵循自然的 Tab 键顺序。在 HTML4 规范中,只有以下元素支持 tabindex 属性: anchor< /a>, 区域, 按钮, 输入,object,选择,以及textarea 。但考虑到可访问性,HTML5 规范允许为所有元素分配
tabindex
。--
例如
相同,
因为不管它们都被赋值
tabindex="1"
,它们仍然会遵循相同的顺序,第一个是第一个,最后一个是最后的。这也是相同的..因为如果它是默认行为,则不需要显式定义 tabIndex。默认情况下,
div
是不可聚焦的,而anchor
标签则可以。The tabindex is used to define a sequence that users follow when they use the Tab key to navigate through a page. By default, the natural tabbing order will match the source order in the markup.
The
tabindex
starts at 0 or any positive whole number and increments upward. It's common to see the value 0 avoided because in older versions of Mozilla and IE, the tabindex would start at 1, move on to 2, and only after 2 would it go to 0 and then 3. The maximum integer value fortabindex
is32767
. If elements have the sametabindex
then the tabindex will match the source order in the markup. A negative value will remove the element from the tab index so it will never be focused.If an element is assigned a
tabindex
of-1
it will remove the element and it will never be focusable but focus can be given to the element programmatically usingelement.focus()
.If you specify the
tabindex
attribute with no value or an empty value it will be ignored.If the
disabled
attribute is set on an element which has atabindex
, the element will be ignored.If a
tabindex
is set anywhere within the page regardless of where it is in relation to the rest of the code (it could be in the footer, content area, where-ever) if there is a definedtabindex
then the tab order will start at the element which is explicitly assigned the lowesttabindex
value above 0. It will then cycle through the elements defined and only after the explicittabindex
elements have been tabbed through, will it return to the beginning of the document and follow the natural tab order.In the HTML4 spec only the following elements support the tabindex attribute: anchor, area, button, input, object, select, and textarea. But the HTML5 spec, with accessibility in mind, allows all elements to be assigned
tabindex
.--
For example
is the same as
because regardless of the fact that they are all assigned
tabindex="1"
, they will still follow the same order, the first one is first, and the last one is last. This is also the same..because you do not need to explicitly define the tabIndex if it's default behavior. A
div
by default will not be focusable, theanchor
tags will.控制页面内的 Tab 键切换顺序(按 tab 键移动焦点)。
参考:http://www.w3.org/TR /html401/interact/forms.html#h-17.11.1
Controlling the order of tabbing (pressing the tab key to move focus) within the page.
Reference: http://www.w3.org/TR/html401/interact/forms.html#h-17.11.1
您设置的值决定键盘焦点在网站上的元素之间移动的顺序。
在下面的示例中,第一次按 Tab 时,光标将移动到 #foo,然后是 #awesome,然后是 #bar
如果您没有在任何地方定义 Tab 索引,键盘焦点将按顺序跟随页面的 HTML 标签它们是在 HTML 文档中定义的。
如果您按 Tab 的次数多于指定的 tabindex 的次数,则焦点将像没有 tabindex 一样移动,即按照 HTML 标记的出现顺序
the values you set determine the order that your keyboard focus will move between elements on the website.
In the following example, the first time you press tab, your cursor will move to #foo, then #awesome, then #bar
If you have not defined tab indexes anywhere, the keyboard focus will follow the HTML tags of you page in the order in which they are defined in the HTML document.
If you tab more times than you have specified tabindexes for, the focus will move as if there were no tabindexes, i.e. in the order of appearance of the HTML tags
它可用于更改默认表单元素焦点导航顺序。
因此,如果您有:
通过使用 Tab 键,您可以浏览 A->B->C。 Tabindex 允许您更改该流程。
It can be used to alter the default form element focus navigation sequence.
So if you've got:
by using the tab key you navigate through A->B->C. Tabindex allows you to change that flow.
通常,当用户在表单中从一个字段切换到另一个字段时(在允许切换的浏览器中,并非所有浏览器都这样做),顺序就是字段在 HTML 代码中出现的顺序。
但是,有时您希望 Tab 键顺序略有不同。在这种情况下,您可以使用 TABINDEX 对字段进行编号。然后选项卡按从最低 TABINDEX 到最高的顺序流动。
方面的更多信息可以在这里找到 w3
有关这 插图可以在此处找到
Normally, when the user tabs from field to field in a form (in a browser that allows tabbing, not all browsers do) the order is the order the fields appear in the HTML code.
However, sometimes you want the tab order to flow a little differently. In that case, you can number the fields using TABINDEX. The tabs then flow in order from lowest TABINDEX to highest.
More info on this can be found here w3
another good illustration can be found here
简单来说,
tabindex
用于关注元素。语法:
tabindex="numeric_value"
这个
numeric_value
是元素的权重。将首先访问较低的值。In Simple words,
tabindex
is used to focus on elements.Syntax:
tabindex="numeric_value"
This
numeric_value
is the weight of element. Lower value will be accessed first.HTML tabindex 属性负责指示是否可以通过键盘导航访问某个元素 。
当用户按下 Tab 键时,焦点将从一个元素转移到另一个元素。通过使用 tabindex 属性,Tab 顺序流会发生变化。
The HTML tabindex atribute is responsible for indicating if an element is reachable by keyboard navigation.
When the user presses the Tab key the focus is shifted from one element to another. By using the tabindex atribute, the tab order flow is shifted.
通过控件进行 Tab 键切换通常按照它们出现在 HTML 代码上的顺序进行。
使用 tabindex,选项卡将从具有最低 tabindex 的控件按 tabindex 顺序流向具有最高 tabindex 的控件
Tabbing through controls usually happens sequentially as they appear on the HTML code.
Using tabindex, the tabbing will flow from control with the lowest tabindex to the control with the highest tabindex in tabindex sequential order