如何在屏幕阅读器上强制取消对元素的焦点?

huangapple go评论80阅读模式
英文:

How do I forcibly remove focus from an element on screen-readers?

问题

I'm working on a single-page app where components get generated and destroyed constantly on the DOM. I'm using the following button with ARIA labels so it's selectable and readable by screen-readers:

<div class="button" role="button" aria-label="Select">Select</div>

In the screenshot below you can see the iOS safari screen-reader focuses on it as expected.

如何在屏幕阅读器上强制取消对元素的焦点?

When the user clicks on the button, I remove it from the DOM. However, the screen-reader focus remains where that button used to be:

如何在屏幕阅读器上强制取消对元素的焦点?

I've tried changing role="none", adding aria-hidden="true", and removing the aria-label for a full second before removing the button:

<div class="button" role="none" aria-hidden="true">Select</div>

... but nothing seems to remove this focus after the button is destroyed.
Is there a way to forcibly remove the focus from this element? Or is there a way to force another element to receive focus instead (like a persistent website logo) so the box doesn't stay floating over an empty space?

英文:

I'm working on a single-page app where components get generated and destroyed constantly on the DOM. I'm using the following button with ARIA labels so it's selectable and readable by screen-readers:

&lt;div class=&quot;button&quot; role=&quot;button&quot; aria-label=&quot;Select&quot;&gt;Select&lt;/div&gt;

In the screenshot below you can see the iOS safari screen-reader focuses on it as expected.

如何在屏幕阅读器上强制取消对元素的焦点?

When the user clicks on the button, I remove it from the DOM. However, the screen-reader focus remains where that button used to be:

如何在屏幕阅读器上强制取消对元素的焦点?

I've tried changing role=&quot;none&quot;, adding aria-hidden=&quot;true&quot;, and removing the aria-label for a full second before removing the button:

&lt;div class=&quot;button&quot; role=&quot;none&quot; aria-hidden=&quot;true&quot;&gt;Select&lt;/div&gt;

... but nothing seems to remove this focus after the button is destroyed.
Is there a way to forcibly remove the focus from this element? Or is there a way to force another element to receive focus instead (like a persistent website logo) so the box doesn't stay floating over an empty space?

答案1

得分: 3

唯一强制取消焦点的方法是将焦点转移到其他地方。焦点始终在某个元素上,它绝不能丢失。

对于键盘用户,焦点必须移到有意义的地方,即任务流程中的下一步元素。

例如,对于模态对话框,焦点必须从触发按钮转移到对话框内的元素,关闭对话框时,焦点必须返回到按钮。

如果没有逻辑上的下一个元素,应重新考虑模式。通常,移除被按下的元素是对任何用户来说相当意外的行为。

还请参考这个讨论:aria-expanded on the button that gets removed from DOM

不要忘记,如果使用自定义按钮而不是真实按钮,还需要处理键盘事件。

英文:

The only way to forcibly remove focus is by putting it somewhere else. Focus is always on some element, it must never get lost.

For keyboard users, it must go somewhere that makes sense, on an element that is the next step in the task flow.

For example, for a modal dialog, focus must go from the triggering button to an element inside the dialog, and when closing it, it must go back to the button.

If there is no logical next element, the patterns should be reconsidered. In general, removing the very element that is pressed is quite unexpected behaviour to any user.

See also this discussion: aria-expanded on the button that gets removed from DOM

Don’t forget that if using a custom button instead of a real one, you also need to handle keyboard events.

huangapple
  • 本文由 发表于 2023年3月9日 14:39:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定