Behavior difference between aria-label and aria-labelledby(咏叹调标记与咏叹调标记的行为差异)
转载作者:bug小助手更新时间:2023-10-27 21:12:31324
I have a toolbar with various buttons with dropdown list. In some cases the button has a label and in other cases it uses an image.
我有一个工具栏与各种按钮的列表。在某些情况下,按钮有一个标签,在其他情况下,它使用图像。
Both are generated to have an aria-labelledby to a containing div that has a title attribute. The screen readers (NVDA, Narrator tested) work fine with the image case, but won't take the aria-labelledby when the button has a label.
But if I switch to aria-label, then it works. In my case that is problematic has the title text is not known yet when the content is first constructed.
However, your code has some unusual coding practices and the aria-label and aria-labelledby are sort of a red herring and have nothing to do with the issue. It's more about trying to compute the accessible name of the <div>.
The name computation has a precedence order. The most "important" or highest precedence attribute is aria-labelledby. It trumps all other attributes. If an aria-labelledby is not found, the next important attribute is aria-label. After that is an element that can provide a name, such as the <label> element. At the very bottom of the precedence tree is the "tooltip" attribute, which is the title attribute.
Your first <div> has a <label> descendant (embedded in the <button>) whereas the other <div> doesn't. So the first <div> is getting its accessible name from the <label> instead of the title because title has the lowest precedence.
您的第一个
有一个
<div title="select the font"> <div> <button> <label>10</label> </button> </div> </div>
If your <div> had an aria-label instead of a title, then that would trump the embedded <label>, but then you might run into a situation where the aria-label of a <div> is ignored unless the <div> also has a role. See the third last bullet point on 2.10 Practical Support: aria-label, aria-labelledby and aria-describedby
Don't use aria-label or aria-labelledby on a span or div unless its given a role.
However, even given all that and if you could get your button to be labelled by the containing <div>, you'd be creating an accessibility issue because of WCAG 2.5.3 Label in Name. That guideline says that the visible label of the button must be contained in the accessible name of the button. The accessible name can contain other text but at a minimum just contain the visible label of the button. In your example, the button displays a "10" but you're trying to get the accessible name of the button to be "select a font...". That would fail. For example, a speech interface user (such as Dragon Naturally Speaking) will see a "10" button but if they say, "click 10", it won't work. They'd have to say, "click select a font" because "select a font" is the accessible name of the button.
thanks for a very detailed response which I am still digesting. Looking at your last paragraph I am wondering what is best. Obviously (IMHO) just saying '10' leaves the user wondering, 10 apples ? 10 houses or whatever. So is that even meeting WGAC guidelines to just read the label?
To satisfy WCAG 2.5.3, if the button displays 10, then the name announced to the screen reader use could say "font size 10". That is, "10" is part of the name because "10" is what is visible. The "font size" addition to the accessible name makes it more clear for the screen reader user.
我是一名优秀的程序员,十分优秀!