Fun with the tabindex attribute

http://www.weba11y.com/blog/2007/11/29/fun-with-the-tabindex-attribute/

 

The tabIndex attribute can be used to allow nearly any element to be put into the tab order or receive focus programmatically. This has been implemented in Internet Explorer starting with version 5, Firefox starting with version 1.5 and is currently being implemented in Opera 9.5.

Being able to set focus to any element on the page is important for accessibility in order to implement full keyboard navigation. Elements that represent the intial interaction with a user interface component

can be but into the tab order. Other elements which are part of a particular user interface component can be interacted with via other key combinations. The keystroke interaction and identification of the user interface components to assistive technologies depends upon being able to set focus to these elements.

How Focus and tab key navigation work

  • Normally only input and anchor elements are put into the tab order of a page by default.
  • Setting a tabIndex attribute of 0 onto an element will put it into the tab order of the page and allow it to receive focus via the keyboard.
  • Setting a tabIndex of -1 on an element will allow the element to receive focus programmatically. For input and anchor elements, setting tabIndex of -1 will remove the element from the tab order and allow focus to only be set programmatically.
  • Setting a tabIndex of >0 onto an element will put that element sequentially into the tab order based on the tabIndex value. Elements with a positive tab index are put into

    the tab order before other elements with a tabIndex of 0 or which are in the tab order by default.

There are different behaviors for the tabIndex attribute and capitalization of the I makes a difference in how the attribute is interpretted in html and xhtml.

After testing different combinations of setting and querying the tabindex attribute, a set of best practices is outlined below followed by a table listing the results of each test.

Using tabIndex across browser and content type

  1. Always use tabindex with lowercase i in markup. See row 1 in table below.
  2. Use tabIndex with uppercase I when setting in script via element.tabIndex. See row 4 in table below.
  3. Check browser and/or content-type when setting in script via element.setAttribute()
    • if is IE or content-type is text/html - set via elem.setAttribute(tabIndex) uppercase. See row 6 in table below.
    • if is content-type application/xhtml+xml or not IE 6 or 7 - set via elem.setAttribute(tabindex) lowercase. See row 7 in table below
  4. Use lowercase when querying a value using getAttribute(tabindex) that was set in markup as lowercase. See row 9 in table below.
  5. Use lowercase when querying a value using getAttribute(tabindex) that was set directly on element using element.tabIndex uppercase. See row 15 in table below.

    Setting tabIndex directly on the element using element.tabIndex avoids having to check browser or content type when getting the value.

  6. If the rules above have been followed, use lowercase when querying a value using hasAttribute(tabindex) where available. See rows 25, 31 and 33 in table below.

Simplified Rules

  • use tabindex lowercase to set in markup
  • use element.tabIndex with uppercase I to set in script
  • query via getAttribute(tabindex) lowercase

TabIndex testing results

The following table shows the results of testing on Windows with Internet Explorer versions 6 and 7, Firefox 2, Firefox 3 (3.0a9pre dated 2007/09/27) and Opera 9.5 alpha (build 9500 dated 2007/09/04).

IE 6 and 7 where tested using HTML 4.01 strict and transitional. Firefox 2 and 3 as well as Opera were tested with HTML 4.01 strict and transitional as well as with XHTML 1.0 strict

served with content-type of application/xhtml+xml. Unless otherwise noted, a div was used as the test element.

 IE 6  IE 7 FF 2 HTMLFF 3 HTMLOpera 9.5alpha HTMLFF 2 XHTMLFF 3 XHTMLOpera 9.5alpha XHTML
1tabindex set in markup (lowercase)yesyesyesyesyesyesyesyes
2tabIndex set in markup (uppercase)yesyesyesyesyesnonono
3tabindex set via elem.tabindex (lowercase)nononononononono
4tabIndex set via elem.tabIndex (uppercase)yesyesyesyesyesyesyesyes
5tabindex set via setAttribute(tabindex) (lowercase)nonoyesyesyesyesyesyes
6tabIndex set via setAttribute(tabIndex) (uppercase)yesyesyesyesyesnonono
7query elem.tabIndex uppercase when set as 0 in markup as lowercase00000000
8query elem.tabIndex uppercase when set as 0 in markup as uppercase00000-1-1-1
9query getAttribute(tabindex) lowercase when set as 0 in markup as lowercase00000000
10query getAttribute(tabIndex) uppercase when set as 0 in markup as lowercase00000nullnullnull
11query getAttribute(tabindex) lowercase when set as 0 in markup as uppercase00000nullnullnull
12query getAttribute(tabIndex) uppercase when set as 0 in markup as uppercase000000 1 0 1 0 1
13query getAttribute(tabindex) lowercase when set via elem.tabindex lowercase00nullnullnullnullnullnull
14query getAttribute(tabIndex) uppercase when set via elem.tabindex lowercase00nullnullnullnullnullnull
15query getAttribute(tabindex) lowercase when set via elem.tabIndex uppercase00000000
16query getAttribute(tabindex) uppercase when set via elem.tabIndexuppercase00000nullnullnull
17query getAttribute(tabindex) lowercase when set via setAttribute as lowercase0 2 0 2 000000
18query getAttribute(tabIndex) uppercase when set via setAttribute as lowercase00000nullnullnull
18query getAttribute(tabindex) lowercase when set via setAttribute as uppercase00000nullnullnull
20query getAttribute(tabIndex) uppercase when set via setAttribute as uppercase000000 3 0 3 0 3
21query elem.tabIndex uppercase when it has not been set in markup00-1-1-1-1-1-1
22query getAttribute(tabIndex) upper or lowercase when it has not been set in markup00nullnullnullnullnullnull
23query elem.tabIndex from <a> with no explict value set00000000
24query getAttribute(tabIndex) upper or lowercase from <a> with no explict value set00nullnullnullnullnullnull
25query hasAttribute(tabindex) lowercase when set in markup as lowercaseNANAtruetruetruetruetruetrue
26query hasAttribute(tabIndex) uppercase when set inmarkup as lowercaseNANAtruetruetruefalsefalsefalse
27query hasAttribute(tabindex) lowercase when set in markup as uppercaseNANAtruetruetruefalsefalsefalse
28query hasAttribute(tabIndex) uppercase when set in markup as uppercaseNANAtruetruetruetrue 1 true 1 true 1
29query hasAttribute(tabindex) lowercase when set via elem.tabindex lowercaseNANAfalsefalsefalsefalsefalsefalse
30query hasAttribute(tabIndex) uppercase when set via elem.tabindex lowercaseNANAfalsefalsefalsefalsefalsefalse
31query hasAttribute(tabindex) lowercase when set via elem.tabIndex uppercaseNANAtruetruetruetruetruetrue
32query hasAttribute(tabindex) uppercase when set via elem.tabIndexuppercaseNANAtruetruetruefalsefalsefalse
33query hasAttribute(tabindex) lowercase when set via setAttribute as lowercaseNANAtruetruetruetruetruetrue
34query hasAttribute(tabIndex) uppercase when set via setAttribute as lowercaseNANAtruetruetruefalsefalsefalse
35query hasAttribute(tabindex) lowercase when set via setAttribute as uppercaseNANAtruetruetruefalsefalsefalse
36query hasAttribute(tabIndex) uppercase when set via setAttribute as uppercaseNANAtruetruetruetruetruetrue
37query hasAttribute(tabIndex) upper or lowercase from <a> with no explict value setNANAfalsefalsefalsefalsefalsefalse

Table Notes:

  1. Setting in markup as uppercase tabIndex does not work in XHTML to allow focus to element.
  2. setAttribute(tabindex) lowercase does not work in IE to allow focus to element.
  3. setAttribute(tabIndex) uppercase does not work in XHTML to allow focus to element.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值