What is the tabindex
attribute used for in HTML?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
-
Why does the box-shadow property not apply to a
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.In Simple words,
tabindex
is used to focus on elements. Syntax:tabindex="numeric_value"
Thisnumeric_value
is the weight of element. Lower value will be accessed first.tabindex
is a global attribute responsible for two things:To my mind the second thing is even more important then 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 to be responsive not only to mouse events but to keyboard events (e.g. 'onkeypress') as well is making such elements focusable. And in the last case 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.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