【问题标题】:Make screen reader ignore a section but read its focusable content让屏幕阅读器忽略一个部分,但阅读其可聚焦的内容
【发布时间】:2019-04-19 16:34:38
【问题描述】:

我有一些看起来或多或少像这样的代码(例如缩减):

html {
  background: #eee;
}

section {
  border-radius: 4px;
  border: 1px solid #ccc;
  background: #fff;
  margin: 1em;
  padding: 1em;
}

[tabindex]:focus {
  outline: 1px dashed blue;
}
<section tabindex="0">
  <h1>
    Section 1
  </h1>
  <p>
    This section will be highlighted but it won't go inside as 
    nothing inside has tabindex. I don't want it to be read at all.
  </p>
</section>

<section tabindex="0">
  <h1>
    Section 2
  </h1>
  <p>
    This section will be highlighted and there is an element inside with 
    tabindex. I don't want the section to be read, but I want to read
    the focusable elements when they are focused.
  </p>
  <p tabindex="0">
    For example: this paragraph has tabindex. It should be read when focused.
  </p>
</section>

当它们获得焦点时会突出显示不同的部分,因为它们有tabindex。这个突出显示可以轻松识别用户在任何给定时刻所处的部分。

在每个部分中都可能有任何内容:文本、html 代码、可聚焦的内容(因为它有 tabindex 或者因为它有交互元素)。这是一个网络组件,我无法控制里面的内容。

我们有屏幕阅读器用户和低视力用户,他们都使用键盘进行导航,对于后者,我们希望帮助确定他们在哪个部分(在具有多个部分/卡片的页面中),而不会对屏幕阅读器用户。

当该部分被突出显示时,我希望它只是图形化的(带有大纲),但我不希望屏幕阅读器阅读它的内容,因为如果里面有任何交互式/可聚焦的内容,它会读两遍,不要重复。

例如:使用 ChromeVox 或 VoiceOver 之类的屏幕阅读器,当按下 tab 时,第一部分将被勾勒出来,听起来像这样:

[Section] 第 1 节。该节将突出显示,但不会进入,因为里面没有任何东西有 tabindex。我根本不希望它被阅读。

再次点击标签,第二部分将被勾勒出来,听起来像:

[Section] Section 2. 这个section会被高亮显示,里面有一个带有tabindex的元素。我不想阅读该部分,但我想在焦点集中时阅读可聚焦的元素。例如:本段有tabindex。应该在专注时阅读。

第三次点击 tab 将激活该部分中的最后一个段落(它有一个tabindex),屏幕阅读器会说:

例如:这个段落有tabindex。应该在专注时阅读。

这种情况并不理想,因为我们正在重复内容(请记住,上面的代码是一个示例,实际上不止于此)。当一个部分获得焦点时,它会被完整地阅读;当里面的内容获得焦点时,它会被再次读取。最后一句话被读了两次:当该部分处于活动状态时和它本身处于活动状态时。

我希望得到以下行为:

  1. tab,第一部分高亮显示。没有读取任何内容。
  2. tab,第二部分高亮显示。没有读取任何内容。
  3. tab,最后一段会突出显示并被阅读。

我尝试通过不同的方法来解决这种行为:

  • 使用role="presentation":所以角色语义不会被映射到可访问性API,也不会被读取...问题是role="presentation" doesn't apply on focusable elements(就像带有tabindex的元素)。
  • 使用role="none":它是role="presentation" 的同义词,因此以同样的方式失败。
  • 使用aria-label="":它会忽略空的aria-label,仍然会读取该部分的全部内容。
  • 使用aria-hidden="true":这样该部分将被忽略并且其内容不会被读取......但它的可聚焦内容也被忽略并且在获得焦点时不会被读取 - 这就是我想要的:李>

html {
  background: #eee;
}

section {
  border-radius: 4px;
  border: 1px solid #ccc;
  background: #fff;
  margin: 1em;
  padding: 1em;
}

[tabindex]:focus {
  outline: 1px dashed blue;
}
<section tabindex="0" aria-hidden="true">
  <h1>
    Section 1
  </h1>
  <p>
    This section will be highlighted but it won't go inside as 
    nothing inside has tabindex. I don't want it to be read at all.
  </p>
</section>

<section tabindex="0" aria-hidden="true">
  <h1>
    Section 2
  </h1>
  <p>
    This section will be highlighted and there is an element inside with 
    tabindex. I don't want the section to be read, but I want to read
    the focusable elements when they are focused.
  </p>
  <p tabindex="0">
    For example: this paragraph has tabindex. It should be read when focused.
  </p>
</section>

我尝试使用aria-hidden="false" 将该部分的全部内容包装到div 中,但似乎父级的aria-hidden="true" 取代了它。

有没有办法实现我想要的?

【问题讨论】:

    标签: html accessibility wai-aria screen-readers


    【解决方案1】:

    简短回答:停止到处添加 tabindex。屏幕阅读器用户使用阅读键进行导航,因此无论您试图做什么来使事情变得可聚焦,都没有任何意义......

    【讨论】:

    • 我同意我们可能过度使用 tabindex,但停止使用它们可能不是解决方案。让我提供更多背景信息:我们有屏幕阅读器用户和低视力用户,他们都使用键盘进行导航,对于后者,我们希望帮助确定他们在哪个部分(在带有部分/卡片的页面内)对屏幕阅读器用户产生负面影响。是否可以在不使用 tabindex 的情况下突出显示部分/卡片?也许我们可以在焦点位于内部时突出显示该部分?但这可能会让人感到困惑,因为它看起来像是同时聚焦了两个元素。
    • 不是他们最初想要的,但我们最终选择的解决方案是从卡片中删除 tabindex。
    【解决方案2】:

    屏幕阅读器用户无需tabindex 即可使用键盘在文本中导航。

    只有交互元素(按钮、链接、表单元素……)可以有tabindex。如果您使用原生交互元素(a[href]buttoninput、...),则不必添加tabindex,仅用于自定义控件(&lt;div role="button" tabindex="0"&gt;click here&lt;/div&gt;)。

    可聚焦元素不应位于另一个可聚焦元素内。

    屏幕阅读器主要有two navigation mode:浏览/阅读模式(通常使用箭头键)和焦点/表单模式(使用tab 键)。屏幕阅读器用户永远不会使用tab 键来阅读非交互式文本。

    您也不需要为当前阅读的文本添加大纲,因为屏幕阅读器会在浏览模式下自动完成。

    【讨论】:

      猜你喜欢
      • 2020-03-30
      • 1970-01-01
      • 2012-04-11
      • 2013-06-20
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多