简体   繁体   English

aria-label在不应该屏幕阅读时被阅读:WCAG

[英]aria-label being read when it should not screen reader : WCAG

The html structure looks like this html结构看起来像这样

<div>
<a href="#"> some info
  <div role="button" tabindex ="0"
      aria-label = "close"
  />
</a>
</div>

When using screen reader the a tag get read "some info close" and then on focus on button it again read "close". 当使用屏幕阅读器的a标签得到读“一些信息关闭”,然后重点按钮,它再次读取“关闭”。 All I want a tag to read is "some info" and button to read "close". 所有我想要a标签是“一些信息”和按钮阅读“关闭”。 What change should I make? 我应该做些什么改变? I cannot change the HTML structure. 我不能改变HTML结构。

jsfiddle https://jsfiddle.net/5c1oywzg/1/ jsfiddle https://jsfiddle.net/5c1oywzg/1/

You can't have a focusable element inside another focusable element. 你不能在另一个可聚焦元素中有一个可聚焦元素。

What change should I make? 应该做些什么改变 I cannot change the HTML structure. 我不能改变 HTML结构。

Being given an HTML code, it's difficult to make a change without changing the code. 获得HTML代码后,很难在不更改代码的情况下进行更改。

  • you're using a a[href] link for what seems to be a button 你正在使用a[href]链接看似什么按钮
  • you have to separate the focusable elements 你必须分开可聚焦的元素

If you could have changed the HTML structure, this would be a better code: 如果您可以更改HTML结构,这将是一个更好的代码:

<div>
  <button> some info</button>
  <button aria-label="close" />
</div>

Unfortunately, we can't change anything if you can't change the HTML structure as your structure is by nature malformed. 遗憾的是,如果您无法更改HTML结构,我们无法更改任何内容,因为您的结构本质上是格式错误的。

We can still use some hacks (using javascript for instance) like adding role=description and tabindex=-1 to the a[href] element and replace "some info" with an html button , but that would be against the second rule of ARIA : 我们仍然可以使用一些hacks(例如使用javascript),例如将role=descriptiontabindex=-1a[href]元素,并用html button替换“some info”,但这将违反ARIA的第二条规则

Do not change native semantics, unless you really have to. 除非你真的需要,否则不要改变原生语义。

1.) The fiddle is different from the code you posted above. 1.)小提琴与您上面发布的代码不同。 For my answer I used the fiddle code (and added a missing " for the href attribute...) 对于我的回答,我使用了小提琴代码(并为href属性添加了一个“缺失" ...)

2.) The button is part of the link, so its content is read as part of the link. 2.)该按钮是链接的一部分,因此其内容将作为链接的一部分进行读取。 Do you really want the (same) link to work both when the button is clicked AND when "some info" is clicked. 当单击按钮和单击“某些信息”时,您是否真的希望(相同)链接同时工作? Looks like "some info" is supposed to be a label/comment for the link? 看起来“某些信息”应该是链接的标签/评论?

depending on what you want, I would either close the a tag before the button or only wrap the button into the a tag, labeling it wth the full text and hiding the text before that with aria-hidden = "true" : 根据你想要的,我要么在按钮之前关闭a标签,要么只将按钮包装到a标签中,用全文标记它并用aria-hidden = "true"文本:

<div>
  <a href="#">
    some info
  </a>
  <button aria-label = "close">close</button>      
</div>

OR 要么

<div>
  <span aria-hidden="true">some info</span>
  <a href="#">
    <button aria-label = "some info, close">close</button>
  </a>
</div>

If you can only add attributes and not change the HTML structure at all you could do this: 如果您只能添加属性而不更改HTML结构,则可以执行以下操作:

<div>
  <a href="#" tabindex="-1">
    <div tabindex="0">some info</div>
    <button>
      close
    </button>
  </a>
</div>

Setting tabindex to -1 removes an element from the tab order while setting it to 0 adds an item. tabindex设置为-1会从Tab键顺序中删除元素,而将其设置为0会添加一个项目。 Generally on other values should be used. 通常应该使用其他值。 More info here . 更多信息在这里

Removing the a tag from the tab order and adding the div in instead will make the keyboard skip over the a tag and focus on both the div and button tags separately. 从Tab键顺序中删除a标签并添加div代替将使键盘跳过a标签并分别关注divbutton标签。

🎻 Fiddle 🎻小提琴

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

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