在Internet Explorer中,如果元素没有tabIndex,它将返回0作为默认值

时间:2019-03-29 11:35:12

标签: javascript internet-explorer dom tabindex

对于此元素,我正在使用javascript获取tabIndex属性

<div role="radio" aria-checked="false" tabindex="-1">
 <div id="if_empty" tabindex="-1" type="radio" value="Show only if empty"></div>
 <label id="radioId" for="if_empty">Show only if empty</label>
</div>

它在除Internet Explorer之外的所有浏览器中均能完美运行。

const element = document.getElementById("radioId");
const tabIndex = element.tabIndex;

// tabIndex is -1 for Firefox/Chrome
// and it is equal 0 for Internet Explorer

如何在Internet Explorer中获取正确的tabIndex?

1 个答案:

答案 0 :(得分:-1)

最适合我的解决方案是使用getAttribute('tabIndex')

获取tabIndex
    for (let i = 0; i < rootNode.childNodes.length; i++) {
      const child = rootNode.childNodes[i];

      if (child.nodeType === 1 && child.getAttribute('tabIndex') === 0) {
             //find tabbable element
      }

    }

如果您不喜欢这个问题,请告诉我,我将其删除。