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

问题描述 投票:0回答:1

我正在使用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?

javascript internet-explorer dom tabindex
1个回答
-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
      }

    }

如果您不喜欢我将删除它的问题,请告诉我..

© www.soinside.com 2019 - 2024. All rights reserved.