Getting, setting, and removing tabindex values with JavaScript

I recently implemented some functions for Dojo for getting, setting, and removing tabindex values. It’s not entirely straightforward — here’s what I found out:

Browsers tested

  • Internet Explorer 6 on Windows XP
  • Internet Explorer 7 on Windows XP
  • Firefox 2 on Windows XP
  • Firefox 3 beta3pre (the Minefield nightly build of 2008-01-07) on Windows XP
  • Safari 3.0.4 on Mac OS X 10.5.1
  • Opera 9.25 on Windows XP

Getting the tabindex value from an element

Element.tabIndex   and  Element.getAttribute("tabindex")   can be used to get the tabindex value on both HTML and XHTML documents:

input with tabindex=”1″IE6 IE7FF2 FF3 HTMLSafari 3 HTMLOpera 9 HTMLFF2 FF3 XHTMLSafari 3 XHTMLOpera 9 XHTML
elem.tabIndex1111111
elem.getAttribute(”tabindex”)1111111
elem.getAttribute(”tabIndex”)1111nullnullnull

Element.getAttribute("tabIndex")   can be used to get the value on HTML documents. However, Firefox, Safari, and Opera treat XHTML documents case-sensitively and, when used on an XHTML document,Element.getAttribute("tabIndex")   will return null.

Getting the tabindex value on an element that doesn’t have one explicitly set

On elements  allowed to have tabindex attributes in HTML4 , the  tabIndex   property defaults to 0 when no tabindex has been explicitly set:

input with no tabindexIE6 IE7FF2 FF3Safari 3Opera 9
elem.tabIndex0000

On elements not allowed to have tabindex attributes in HTML4 (but  supported by IE and Firefox   and the upcoming HTML5), the  tabIndex   property defaults to 0 on Internet Explorer, -1 on Firefox, and undefined on Safari 3 and Opera 9:

div with no tabindexIE6 IE7FF2 FF3Safari 3Opera 9
elem.tabIndex0-1undefinedundefined

On Internet Explorer,  Element.getAttribute("tabindex")   returns 0 on elements that have no tabindex value explicitly set. The other browsers return null:

div with no tabindexIE6 IE7FF2 FF3Safari 3Opera 9
elem.getAttribute(”tabindex”)0nullnullnull

Testing if tabindex has been set on an element

On Internet Explorer we can’t use  Element.getAttribute("tabindex")   to determine if a tabindex has been specified on an element because it returns 0 even when one hasn’t been. IE also doesn’t implementElement.hasAttribute() . However, IE6 and IE7 do implement  Element.getAttributeNode()   and we can use that (Element.getAttributeNode()   is also available on Firefox, Safari, and Opera.) When it is called on Internet Explorer on an element with no tabindex value set,  Element.getAttributeNode()   will return a node whosespecified   property is false. When called on Firefox, Safari, and Opera it will return  null . For cross-browser compatibility, we can use a test like this:

var attr = elem.getAttributeNode("tabindex");
return attr ? attr.specified : false;

Setting the tabindex value on an element

Element.setAttribute("tabIndex")   must be used on Internet Explorer. Either “tabindex” or “tabIndex” can be used for HTML on Firefox, Safari, and Opera, but “tabindex” must be used for XHTML:

input elementIE6 IE7FF2 FF3 HTMLSafari 3 HTMLOpera 9 HTMLFF2 FF3 XHTMLSafari 3 XHTMLOpera 9 XHTML
elem.setAttribute(”tabindex”)NYYYYYY
elem.setAttribute(”tabIndex”)YYYYNNN

Removing the tabindex value from an element

Like  setAttribute() , Element.removeAttribute(”tabIndex”)   must be used on Internet Explorer and either “tabindex” or “tabIndex” can be used for HTML on Firefox, Safari, and Opera. “tabindex” must be used for XHTML:

input elementIE6 IE7FF2 FF3 HTMLSafari 3 HTMLOpera 9 HTMLFF2 FF3 XHTMLSafari 3 XHTMLOpera 9 XHTML
elem.removeAttribute(”tabindex”)NYYYYYY
elem.removeAttribute(”tabIndex”)YYYYNNN

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值