简体   繁体   English

屏幕阅读器 - 仅阅读具有 role="alert" 而不是焦点的元素

[英]Screen reader - Only read element with role="alert" and not on focus

I'm building a filter for a site that needs to work with a screen reader and i'm currently facing this problem.我正在为需要使用屏幕阅读器的网站构建过滤器,而我目前正面临这个问题。

The filter is built in a modal that houses a set of checkboxes and one button that displays how many hits the user has got after selecting each checkbox.该过滤器构建在一个模态中,该模态包含一组复选框和一个按钮,该按钮显示用户在选中每个复选框后获得的点击次数。 The text in the button is dynamic and changes depending on the number of hits there is after a checkbox has been selected.按钮中的文本是动态的,并且会根据选中复选框后的点击次数而变化。

The text in the button says "Show X hits".按钮中的文字显示“显示 X 次匹配”。 I have added a role="alert" to the button so that the screen reader pick ups imidiatly if the value/text changes in the button.我在按钮上添加了一个 role="alert" ,以便屏幕阅读器在按钮中的值/文本发生变化时立即获取。

My question is, is it possible for the screen reader to read the text "X hits" when the button is triggerd by the role="alert" and the text "Show X hits" when the user navigtes down to the button?我的问题是,当按钮被 role="alert" 触发时,屏幕阅读器是否可以阅读文本“X hits”,而当用户向下导航到按钮时,文本“Show X hits”?

Example of my button:我的按钮示例:

<button type="button" role="alert" class="btn btn-primary" v-on:click="search()">
   <span class="sr-only">{{amount}} hits.</span>
   <span>Show {{amount}} hits.</span>
</button>

UPDATE Managed to get a working solution using this:更新设法使用此方法获得有效的解决方案:

<button type="button" class="btn btn-primary" v-on:click="search()">
   <span aria-hidden="true">Show {{amount}} hits.</span>
      <div class="sr-only">
          <span>Show</span>
          <span role="alert">{{amount}} hits</span>
      </div>
</button>

You should not put a live region inside a button.您不应该将活动区域放在按钮内。 The button role has presentational children . button角色有presentational children

That means that, as per ARIA standard, the role of any child of the button should be ignored.这意味着,根据 ARIA 标准,应该忽略按钮的任何子项的角色。 This includes role="alert" .这包括role="alert"

Maybe it works in your browser/screen reader combination, but I wouldn't rely on that.也许它适用于您的浏览器/屏幕阅读器组合,但我不会依赖它。

I don't see a reason why the text would be inside the button anyway, since your are hiding it.我看不出文本为什么会在按钮内的原因,因为你隐藏了它。

So I'd recommend this:所以我推荐这个:

<button type="button" class="btn btn-primary" v-on:click="search()">
  Show {{amount}} hits.
</button>
<div class="sr-only" role="alert">{{amount}} hits</div>

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

相关问题 在ember中将屏幕阅读器的焦点设置为模态警报 - Setting focus of a screen reader on a modal alert in ember 如果初始焦点设置为另一个元素,如何让屏幕阅读器在安装时读取组件中的标题? - How to make screen reader read a heading in a component on mount if initial focus is set to another element? 如何停止屏幕阅读器以读取具有角色 =“警报”的所有错误跨度标记? - How to stop Screen reader to reads all errors span tag which has role=“alert”? 屏幕阅读器和HTML文档重点 - Screen reader and HTML document focus 控制屏幕阅读器 - 让屏幕阅读器在呈现特定元素时进行阅读 - Control the screen reader - Make screen reader to read when a particular element has rendered 专注于仅使用警报 - focus only working with alert NVDA屏幕阅读器无法预测切换到对焦模式 - NVDA screen reader not switching to focus mode predictably 我将反应状态呈现为可访问的元素。 一旦按下更改状态的按钮,如何强制屏幕阅读器聚焦/重新阅读元素? - I render react state as an accessible element. How to force the screen reader to focus/reread the element once a button that changes the state is hit? 只关注第一个元素 - Only focus in the first element 如何通过屏幕阅读器读取输入然后标签? - How to read input then label via screen reader?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM