Показать сообщение отдельно
  #7 (permalink)  
Старый 08.10.2023, 16:40
Аватар для ruslan_mart
Профессор
Отправить личное сообщение для ruslan_mart Посмотреть профиль Найти все сообщения от ruslan_mart
 
Регистрация: 30.04.2012
Сообщений: 3,018

С ссылками (якорями) похожая история, кстати.

Ссылка по умолчанию имеет tabindex 0, но при этом, если отсутствует `href`, то она не табается, не смотря на то, что tabindex все еще 0. Но если явно его переопределить, то все начинает работать правильно.


<a id="link" href="">Табается</a>


<script>
console.log(link.tabIndex); // 0
</script>




<a id="link">Не табается</a>


<script>
console.log(link.tabIndex); // 0
</script>


<a id="link">табается</a>


<script>
console.log(link.tabIndex); // 0
link.tabIndex = link.tabIndex;
</script>




------

Угадайте какой tabIndex будет здесь

<a contenteditable="true">123</a>


------

Финальный костыль:

function getRealTabIndex(element: HTMLElement) {
  // Anchors without the "href" attribute have a default tab index of 0, although they behave like -1.
  // If this is the case, then treat the tabindex as -1
  if (
    element instanceof HTMLAnchorElement &&
    !element.hasAttribute('href') &&
    !element.hasAttribute('tabindex')
  ) {
    return -1;
  }

  // ContentEditable elements have a default tabindex of -1, although they behave like 0.
  // If this is the case, then treat the tabindex as 0
  if (element.isContentEditable && element.tabIndex === -1 && !element.hasAttribute('tabindex')) {
    return 0;
  }

  return element.tabIndex;
}

Последний раз редактировалось ruslan_mart, 08.10.2023 в 16:50.
Ответить с цитированием