简体   繁体   English

Aria-label 不适用于带有 NVDA 的 Chrome 和 Firefox

[英]Aria-label is not working in Chrome and Firefox with NVDA

I have a simple textbox:我有一个简单的文本框:

<input type="text" aria-label="First Name" title="First Name" />

I am showing the tooltip First Name on hover of textbox.我在文本框悬停时显示工具提示First Name

I used the aria-label as well as aria-labelledby but neither are working with Chrome or Firefox.我使用了aria-labelaria-labelledby但都不适用于 Chrome 或 Firefox。

It's working on selection of the textbox, but not working on mouse hover.它正在处理文本框的选择,但不适用于鼠标悬停。

But it's working fine with IE on mouse hover as well as on textbox select.但它在鼠标悬停和文本框选择上与 IE 一起工作正常。

I am using the screen reader NVDA.我正在使用屏幕阅读器 NVDA。

Just to avoid confusion the correct method for labelling an input is to simply use a <label> .为了避免混淆,标记输入的正确方法是简单地使用<label>

So you should have所以你应该有

<label for="firstName">First Name</label>
<input id="firstName" name="firstName" type="text"/>

The way to link labels and inputs is using the for attribute and point that at the input's ID.链接标签和输入的方法是使用for属性并将其指向输入的 ID。

The added benefit of this is that if you click on the label it will focus the corresponding input , which other solutions will not.这样做的额外好处是,如果您单击label ,它将聚焦相应的input ,而其他解决方案则不会。

Should you for some reason require an input without a label then the following example illustrates how to do this correctly.如果您出于某种原因需要没有标签的输入,那么以下示例说明了如何正确执行此操作。 ( please do not do this if you can avoid it, labels are important for people with anxiety disorders to be able to check that they have filled in the correct field - however I know that sometimes 'designers' just won't budge and you have to workaround them....) (如果可以避免,请不要这样做,标签对于焦虑症患者来说很重要,可以检查他们是否填写了正确的字段 - 但是我知道有时“设计师”不会让步,而您有解决它们....)

In this example we 'visually hide' the label using CSS and add placeholder text to the element.在此示例中,我们使用 CSS 以“视觉方式隐藏”标签并向元素添加placeholder文本。 Just to reiterate this is a last resort for those designers who will not listen about accessibility and you should use visible labels.重申一下,对于那些不会听取可访问性并且您应该使用可见标签的设计师来说,这是最后的手段

At least doing it this way the input will function correctly for screen reader users.至少通过这种方式,屏幕阅读器用户的输入将正常运行。

 .visually-hidden { position: absolute !important; height: 1px; width: 1px; overflow: hidden; clip: rect(1px 1px 1px 1px); /* IE6, IE7 */ clip: rect(1px, 1px, 1px, 1px); white-space: nowrap; /* added line */ }
 <label for="firstName" class="visually-hidden">First Name</label> <input id="firstName" name="firstName" placeholder="First Name" type="text"/>

Edit编辑

Finally got chance to test this, works in Firefox, Internet Explorer and not in Chrome for announce on hover.终于有机会测试这个,在 Firefox、Internet Explorer 中工作,而不是在 Chrome 中用于悬停时宣布。

However if the label is visible it does work fine (if you hover the label, it does not announce if you hover the input itself even with a visible label), it also works fine for if you focus the input .但是,如果标签可见,它确实可以正常工作(如果您将标签悬停在标签上,即使您将输入本身悬停在可见标签上,它也不会宣布),如果您focus input ,它也可以正常工作。

Final thoughts - show the labels (third time I said this in one answer. hehe), problem solved, no need to make it complicated.最后的想法 -显示标签(我在一个答案中第三次说了这个。呵呵),问题解决了,不需要让它复杂化。

Also, I am not sure why you think this is important, if someone uses a screen reader to assist while using a mouse they will click on an input, I have never come across anyone who would find not having a form field read on hover an accessibility issue if it works correctly once you click into the field.另外,我不确定您为什么认为这很重要,如果有人在使用鼠标时使用屏幕阅读器来提供帮助,他们会点击输入,我从未遇到过任何人会发现悬停时没有读取表单字段如果单击该字段后它可以正常工作,则可访问性问题。

Also the only people possibly affected by this are:-此外,唯一可能受此影响的人是:-

  • screen reader users,屏幕阅读器用户,
  • who use the mouse,谁使用鼠标,
  • who have a sight impediment,有视力障碍的人,
  • who use Google Chrome,谁使用谷歌浏览器,
  • who also use NVDA,谁也使用 NVDA,
  • and do not use a screen magnifier.并且不要使用屏幕放大镜。

pretty specific, so not likely an issue.非常具体,所以不太可能成为问题。

Its working on selection of textbox, but not working on mouse hover.它适用于选择文本框,但不适用于鼠标悬停。

It's not intended to work on mouse hover.它不适用于鼠标悬停。

NVDA reads the label for input elements on mouse hover, not the accessible name. NVDA 在鼠标悬停时读取输入元素的标签,而不是可访问的名称。

If you want something to be read, you have to add a label.如果您想阅读某些内容,则必须添加标签。

I tested with NVDA on Firefox and Chrome and I can confirm the screen reader doesn't announce the value of aria-label on the input我在 Firefox 和 Chrome 上使用 NVDA 进行了测试,我可以确认屏幕阅读器没有在input宣布aria-label的值

I looked at 2.10 Practical Support: aria-label, aria-labelledby and aria-describedby and found out that aria-label isn't supported for input elements, but aria-labelledby and aria-describedby are.我查看了2.10 Practical Support: aria-label, aria-labelledby 和 aria-describedby ,发现input元素不支持aria-label ,但aria-labelledbyaria-describedby Taken from the link above:取自上面的链接:

aria-labelledby and aria-describedby are robustly supported for interactive content elements such as links and form controls including the many input types. aria-labelledby 和 aria-describesby 为交互式内容元素提供了强大的支持,例如链接和表单控件,包括许多输入类型。

So I changed the code this snippet works with NVDA on Chrome and Firefox.因此,我更改了此代码段在 Chrome 和 Firefox 上与 NVDA 一起使用的代码。

<input type="text"  aria-labelledby="label" />
<span id="label">First Name</span>

Make sure to enclose the input and the "label" inside form for it to work optimally.确保将input和“标签”包含在form内, form使其最佳工作。

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

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