英文:
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:
<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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论