简体   繁体   English

无法在移动Safari中为带有子项的tabindexed元素设置focus()?

[英]Can't set focus() on a tabindexed element with children in mobile Safari?

As part of some a11y work I've been doing recently on an one page app, I've added code that calls focus() on the header of a page whenever a new page loads. 作为我最近在一页应用程序上所做的所有工作的一部分,我添加了代码,每当加载新页面时,该代码就会在页面的标题上调用focus() This works great on desktop, and gets read out correctly by a screenreader, but completely fails with VoiceOver in mobile Safari. 这在台式机上效果很好,并且可以通过屏幕阅读器正确读取,但是在移动Safari中使用VoiceOver会完全失败。

After hours of debugging and Googling, I discovered that it seems to fail when trying to focus on an element (that is normally not-focusable) with children. 经过数小时的调试和谷歌搜索,我发现尝试着重于带有子元素的元素(通常是不可聚焦的)时似乎失败了。

Example code: 示例代码:

<div id="header1" tabindex="-1">Hello world</div>
<div id="header2" tabindex="-1">Goodbye <span>cruel cruel</span> world</div>

Then enable VoiceOver in Safari, and try to call: 然后在Safari中启用VoiceOver,然后尝试调用:

document.getElementById('header1').focus();

And notice that it gets read, but that 并注意它已被读取,但是

document.getElementById('header2').focus();

does absolutely nothing, which seems like completely broken behavior to me. 什么也没做,对我来说似乎完全是坏事。 Am I doing something fundamentally wrong, or is this a known issue, and is there any way around this without having to resort to focusing on elements without children? 我是在做根本上是错误的事情,还是这是已知问题,是否有办法解决这一问题而不必求助于没有孩子的元素?

That does sound messed up. 听起来确实很混乱。 You can try two different things: 您可以尝试两种不同的方法:

  1. add a tabindex="-1" to the inner <span> . 在内部<span>添加一个tabindex="-1" I doubt that will do anything but perhaps the non-focusable nature of the inner element is affecting the parent element. 我怀疑这会做什么,但是内部元素的不可聚焦性可能会影响父元素。

  2. use the undocumented role="text" on the outer <div> . 在外部<div>上使用未记录的role="text" That will cause the entire <div> to be treated as one element instead of treating the inner element as a separate VoiceOver "tab stop". 这将导致整个<div>被视为一个元素,而不是将内部元素视为单独的VoiceOver“制表位”。 This probably has a better chance at fixing the problem. 这可能有更好的机会解决此问题。

<div role="text" id="header2" tabindex="-1">Goodbye <span>cruel cruel</span> world</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM