简体   繁体   English

屏幕阅读器应读取 aria-label 并忽略带有 for 属性的标签

[英]Screenreader shall read aria-label and ignore label with for attribute

I am currently working on an html form with proper disability access.我目前正在处理具有适当残疾访问权限的 html 表单。 I have inputs labelled by labels with the for-attribute.我的输入由带有 for 属性的标签标记。 But now I want one input getting a different text for the screenreader than the label displays:但是现在我想要一个输入为屏幕阅读器获取与标签显示不同的文本:

<div class="cc_form_w50 t5">
  <label id="lbl_city" for="input_city">City / Town</label>
</div>
<div class="cc_form_w50 t5">
  <input type="text" required name="city" title="City / Town" placeholder="Enter your city or town" class="w100" id="input_city">
</div>

The screenreader ready the "/" as symbol in the system language, so I want to make the screenreader "or" instead, like the title or placeholder.屏幕阅读器准备好“/”作为系统语言中的符号,所以我想让屏幕阅读器变成“或”,就像标题或占位符一样。 As long as I use the "for"-attribute or "aria-labelledby" the text of the label is read.只要我使用“for”-attribute 或“aria-labelledby”,就会读取标签的文本。 Any "aria-label"-attribute is ignored by the reader.任何“aria-label”属性都会被阅读器忽略。

Without the "for"-attribute the cursor doesn't enter the input by selecting the label.如果没有“for”属性,光标不会通过选择标签进入输入。

Is it possible to tell the screenreader to read something else than the content of the label-tag?是否可以告诉屏幕阅读器阅读标签标签内容以外的其他内容?

There are two ways to fix it.有两种方法可以修复它。

The first hides the '/' from screen readers (using aria-hidden ) then adds visually hidden text that is read by screen readers.第一个隐藏屏幕阅读器的“/”(使用aria-hidden ),然后添加屏幕阅读器阅读的视觉隐藏文本。 You can do a google search on the sr-only class.您可以在sr-only类上进行谷歌搜索。 It comes from bootstrap but lots of other frameworks define it too.它来自 bootstrap,但许多其他框架也定义了它。 You can copy the definition of the class from this stackoverflow answer .您可以从此stackoverflow answer复制类的定义。

<div>
  <label for="input_city">City <span aria-hidden="true">/</span> <span class="sr-only">or</span> Town</label>
</div>
<div>
  <input type="text" required name="city" placeholder="Enter your city or town" id="input_city">
</div>

Another way to fix it (and this is a little simpler) is to hide the label completely (again, using aria-hidden ) from the screen reader then specify an aria-label on the <input> .另一种修复它的方法(这有点简单)是从屏幕阅读器中完全隐藏标签(再次使用aria-hidden ),然后在<input>上指定一个aria-label

<div>
  <label for="input_city" aria-hidden="true">City / Town</label>
</div>
<div>
  <input type="text" required name="city" placeholder="Enter your city or town" id="input_city" aria-label="city or town">
</div>

Both solutions still allow the mouse user to click on the label and have it move the focus to the <input> field.这两种解决方案仍然允许鼠标用户单击标签并将焦点移动到<input>字段。

I also removed the title (tooltip) attribute because it seemed overkill having a label, a placeholder, and a tooltip.我还删除了title (工具提示)属性,因为它看起来有点矫枉过正,有标签、占位符和工具提示。 Plus, some screen readers incorrectly include the tooltip in the name of the label when it's read, so sometimes you still hear the '/' in the label if you have the tooltip.此外,某些屏幕阅读器在阅读时错误地将工具提示包含在标签名称中,因此如果您有工具提示,有时您仍然会听到标签中的“/”。 (The tooltip is the last attribute examined when determining the accessible name of an element. See step 2I in the " Accessible Name and Description Computation ") (工具提示是在确定元素的可访问名称时检查的最后一个属性。请参阅“可访问名称和描述计算”中的步骤 2I)

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

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