简体   繁体   English

使用 role=option 时,让屏幕阅读器读出前缀文本和 DOM 内容

[英]Make Screen Reader read out a prefix text along with DOM contents when using role=option

I have an outer div of role="listbox" and a few inner divs of role="option" .我有一个role="listbox"的外部 div 和一些role="option"的内部 div。 When I use keyboard up-down to move focus within the elements of the inner div, it reads them out as "1 of 5, Element 1", "2 of 5, Element 2", "3 of 5, Element 3" and so on..当我使用上下键盘在内部 div 的元素内移动焦点时,它会将它们读出为“1 of 5, Element 1”、“2 of 5, Element 2”、“3 of 5, Element 3”和很快..

I am looking to have it read a prefix text such as "Prefix text" prior to it reading out the element, so it reads as "Prefix text, 1 of 5, Element 1", "Prefix text, 2 of 5, Element 2" and so on..我希望它在读出元素之前读取前缀文本,例如“前缀文本”,因此它显示为“前缀文本,5 个中的 1 个,元素 1”,“前缀文本,5 个中的 2 个,元素 2” “ 等等..

I have tried adding an aria-label="prefix text" to the outer div but it doesn't help.我试过向外部 div 添加aria-label="prefix text"但它没有帮助。

I have a codesandbox here showing the DOM structure - https://codesandbox.io/s/narrator-option-example-eb8ghh?file=/index.html我这里有一个显示 DOM 结构的 codesandbox - https://codesandbox.io/s/narrator-option-example-eb8ghh?file=/index.html

First clarifying question, when you say you use the up-down arrow key, is that with or without a screen reader running?首先澄清问题,当你说你使用上下箭头键时,是在运行还是不运行屏幕阅读器的情况下? Because up-down when a screen reader is running moves the screen reader focus to the next DOM element but up-down without the screen reader will scroll the page unless you are trapping on that keyboard event.因为在屏幕阅读器运行时 up-down 会将屏幕阅读器焦点移动到下一个 DOM 元素,但是在没有屏幕阅读器的情况下 up-down 将滚动页面,除非您陷入该键盘事件。

I ask because your code sample doesn't seem to handle keyboard events so without a screen reader running, I can't navigate to the listbox items.我问是因为您的代码示例似乎无法处理键盘事件,所以如果没有运行屏幕阅读器,我无法导航到列表框项目。

However, with that being said, you can trick the screen reader into saying prefix text but not display the prefix text using a CSS content property.但是,话虽如此,您可以使用 CSS content属性欺骗屏幕阅读器说出前缀文本而不显示前缀文本。

In your demo code, add this:在您的演示代码中,添加以下内容:

<style>
  div[role=option]::before {content: "prefix text";font-size: 0px;}  
</style>

Setting the font size to 0 makes it invisible but the screen reader still announces it.将字体大小设置为 0 使其不可见,但屏幕阅读器仍会宣布它。 It works with NVDA, JAWS, and Voiceover.它适用于 NVDA、JAWS 和 Voiceover。

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

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