简体   繁体   English

HTML表格中隐藏的咏叹调标签(用于屏幕阅读器辅助功能)

[英]Hidden aria labels in HTML table (for screen reader accessibility)

In my webpage, I need to create a table which has a header row which is visible or hidden according to some user configuration. 在我的网页中,我需要创建一个表,该表具有根据某些用户配置可见或隐藏的标题行。 This table also needs to be fully accessible (specifically, since the table can be very long, I would like that shortcuts that read the row/column headers would work ). 此表也需要完全可访问(具体来说,因为表可能很长,我希望读取行/列标题的快捷方式可以工作 )。 I only have ChromeVox to test with (I'll detail about behaviors with other readers from blog-posts I found). 我只有ChromeVox进行测试(我将详细介绍与我发现的博客文章中其他读者的行为)。

My current layout looks similar to this one: 我目前的布局与此类似:

CSS: CSS:

.table-header-show {
}

.table-header-hide {
  display: none;
}

HTML: HTML:

<table>
  <!-- ${show} is used to choose the right class the user configuration -->
  <thead class="table-header-${show}">
    <tr>
      <th>Name</th>
      <th>Value 1</th>
      <th>Value 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>John Doe</td>
      <td>Value 1a</td>
      <td>Value 2a</td>
    </tr>
  </tbody>
</table>

When the header is visible, there is no problem at all. 当标题可见时,根本没有问题。 When the header is hidden, it depends on the screen reader whether these labels are read out or not: 标题隐藏后,取决于屏幕阅读器是否读取这些标签:

  • I would like the header row to be skipped when using regular navigation* with the screen reader, but use the header row to announce column labels 我想在屏幕阅读器上使用常规导航*时跳过标题行 ,但是使用标题行来宣布列标签
  • With ChromeVox, the first works (skipped in navigation) but the second fails (the hidden row is not used to label columns) 使用ChromeVox,第一个工作(跳过导航),但第二个失败(隐藏的行不用于标记列)
  • Again with ChromeVox, moving the hiding to be declared as a style attribute rather than a class , cause both desired behaviors to work 再次使用ChromeVox,将隐藏声明移动到style属性而不是class ,会导致所需的行为都起作用
  • According to a blog post I found, screen readers somtimes ignore display: none in order to speak out content, and sometimes they do not - so I'm not sure I can rely on this sort of hiding to be reliable for my purpose (hide for navigation, use for labeling) 根据我发现的博客文章, 屏幕阅读器有时会忽略display: none来说出内容,有时候却没有 - 所以我不确定我可以依靠这种隐藏来保证我的目的是可靠的(隐藏用于导航,用于标签)

So, how can I achieve my desired behavior in a cross-browser-reader way? 那么,我如何以跨浏览器阅读器的方式实现我想要的行为呢?

  • AFAIK, the problem with off-screen/1px-sized hiding (as suggested here ) is that if I do this for the header row, then all the column headers would always be read out... AFAIK,屏幕外/ 1px大小的隐藏问题(如此处所示 )是,如果我为标题行执行此操作,那么将始终读出所有列标题...

The most fool proof option will be to hide the header rows from the screen reader, either by removing them from the DOM (preferred) or with a screen-reader safe removal. 最简单的选项是隐藏屏幕阅读器中的标题行,方法是将其从DOM(首选)中删除,或者使用屏幕阅读器安全删除。 Then insert the header into each applicable cell with text-indent: -999em; 然后使用text-indent: -999em;将标题插入每个适用的单元格text-indent: -999em; or similar visual hiding that preserves screen reader access. 或类似的视觉隐藏,保留屏幕阅读器访问。

This is a very unsatisfying answer, but it should be tenable with the jquery. 这是一个非常不满意的答案,但它应该与jquery成立。 Roughly: for each th , store content in an array, then select each td in the applicable column, and .prependTo() the 'th'. 大致为:对于每个th阵列中的,存储的内容,然后选择在适用的列中的每个TD,和.prependTo()的“日”。

Again, very unsatisfying and messy but the only fool proof solution I can think of. 再次,非常不满意和凌乱,但我能想到的唯一傻瓜证明解决方案。 Best of luck. 祝你好运。

This might help you. 这可能对你有帮助。

<table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Value 1</th>
                    <th id="foo">Value 2</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>John Doe</td>
                    <td>Value 1a</td>
                    <td id="foo">Value 2a</td>
                </tr>
            </tbody>
        </table>


#foo {
        visibility: collapse
    }

But About above mention code don't have all browser support. 但是关于上面提到的代码没有所有的浏览器支持。

To improve what you have, you could use table-layout: fixed on table because of table- layout when table-cell is not display: none it will automatically display: table-cell 为了改进你所拥有的,你可以使用table-layout: fixed在表上,因为table-cell不display: none时表格布局display: none它会自动display: table-cell

Instead of display: none on the head - try moving off-screen by taking it out of the flow - something like: 而不是display: none头部display: none - 尝试通过将其从流程中移出而移出屏幕 - 例如:

<table class="hidden-header">
    ...

.hidden-header thead {
    display: absolute;
    left: -9999
}

I have not tested this ... but might work. 我没有测试过这个...但可能会有效。

Edit: Further reading: 编辑:进一步阅读:

A quick google game me the following articles i recommend: 快速谷歌游戏我推荐以下文章:

Why don't you send this as a question to the ShopTalk Show popcast for one of their quick-fire episodes? 你为什么不把这个问题作为一个问题发送到ShopTalk Show popcast上,用于他们的一个快速剧集?

I'm interested in the answer here as well, and I hope I understand the question. 我也对这里的答案感兴趣,我希望我理解这个问题。 What if you use <colgroup> and <col> tags and give them an aria-label ? 如果您使用<colgroup><col>标签并给它们一个aria-label怎么办? ChromeVox reads them and they are semantically tied to the columns. ChromeVox会读取它们,并且它们在语义上与列相关联。

Here's an example in jsfiddle: http://jsfiddle.net/j87x7sn5/4/ 这是jsfiddle中的一个例子: http//jsfiddle.net/j87x7sn5/4/

This code uses sibling selectors to "hide" the colgroups when the header is visible and "show" them when it is not so the screenreader doesn't read it twice. 此代码使用同级选择器在标题可见时“隐藏”colgroup,并在不显示时“显示”它们,因此屏幕阅读器不会读取它两次。

It won't hurt to use aria-hidden="false" if ChromeVox is currently your only concern. 如果ChromeVox是您唯一关心的问题,那么使用aria-hidden =“false”也不会有什么坏处。 You can use it preferably in combination with the HTML5 hidden attribute (see example below) but other techniques might work as well. 您最好结合HTML5隐藏属性使用它(参见下面的示例),但其他技术也可以使用。 See this article from late 2013 for compatibility of the different techniques. 请参阅2013年末的这篇文章 ,了解不同技术的兼容性。 If compatibility is a concern, the off screen method seems to be the way to go. 如果兼容性是一个问题,屏幕外方法似乎是要走的路。

Off screen method: 屏幕外方法:

<style>
    thead {
        position:absolute;
        left:-9999px
    }
</style>

Hidden method: 隐藏方法:

<table>
  <!-- ${show} is used to choose the right class the user configuration -->
  <thead hidden aria-hidden="false">
    <tr>
      <th>Name</th>
      <th>Value 1</th>
      <th>Value 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>John Doe</td>
      <td>Value 1a</td>
      <td>Value 2a</td>
    </tr>
  </tbody>
</table>

Instead of Display None what if use opacity 0 , 而不是显示无如果使用不透明度0,

i tried to Google this for any clear answer but didn't get any clear one but still i can assume Screen Reader ignore opacity and read Table Labels as normal and its not visible for Normal Users. 我试图谷歌这个任何明确的答案,但没有得到任何明确的答案,但我仍然可以假设屏幕阅读器忽略不透明度并正常阅读表格标签,它对普通用户不可见。

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

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