【问题标题】:Accessibility: Help text for input elements accessibility可访问性:输入元素可访问性的帮助文本
【发布时间】:2023-06-18 22:49:01
【问题描述】:

我有一个文本input,它要求用户输入具有特定约束的密码。这些约束的帮助文本在单独的 DOM 元素中指定。

我希望屏幕阅读器在用户关注输入字段时阅读此帮助文本。我找到了两种方法:

  1. 在输入文本上使用aria-describedby="help-text-id"

    <label for="password"> Password </label>
    <input type="text" aria-describedby="help-text" id="password" />
    <span id="help-text"> The password should at least be 8 characters</span>
    
  2. 在输入文本上使用aria-labelledby="label-id help-text-id"读取对应的标签以及帮助文本作为标签


    <label id="password-label"> Password </label>
    <input type="text" aria-labelledbyby="password label help-text" />
    <span id="help-text"> The password should at least be 8 characters</span>

我喜欢第一个,因为从语义上讲,此帮助文本是描述输入的内容,而不是标记输入的内容。 我只是不确定这是否是处理此问题的正确方法。还有其他的帮助文本发布模式吗?

另外,aria-describedby 似乎不适用于 ChromeVox 扩展程序,也不适用于 Windows 10 屏幕阅读器。

参考:https://developer.paciellogroup.com/blog/2018/09/describing-aria-describedby/

【问题讨论】:

    标签: html accessibility


    【解决方案1】:

    您始终可以在与input 元素关联的label 中包含帮助文本。这将保证与任何屏幕阅读器/浏览器组合 100% 兼容。

    除了总体说明外,在表单控件的标签中提供相关说明也很重要。例如,在标签的文本中指示所需的输入字段和数据格式。 https://www.w3.org/WAI/tutorials/forms/instructions/

    您甚至可以在 label 中使用 use CSS to offer this text only to screen reader users,因为这是您在原始示例中提供的行为。

    在您提供的两种解决方案中,我会选择aria-describedby,因为它提供了更高级别的supported usage across browsers

    【讨论】:

    • 感谢您的链接。对于这种特殊用法,我可以看到 aria-labelledby 在与 input type="text" 一起使用时具有更好的支持