简体   繁体   English

辅助功能和 Angular:使用 aria-expanded 和 aria-controls 的正确方法 - 在所描述的几个选项中哪些选项是可接受的

[英]Accessibility and Angular: proper way to use aria-expanded and aria-controls - what options are acceptable among several described

If I validate this code on accessibility issues here https://validator.w3.org/nu/#textarea , then I don't have any errors:如果我在这里https://validator.w3.org/nu/#textarea验证此代码的可访问性问题,那么我没有任何错误:

<button aria-controls="t1" aria-expanded="false">Show</button>
<div id="t1">Some text</div>

Unfortunately, I use conditions ( *ngIf in Angular) which removes the <div> from the DOM until aria-expanded is true (myBoolValue is false when page loads):不幸的是,我使用条件(Angular 中的 *ngIf)从 DOM 中删除<div>直到 aria-expanded 为真(页面加载时 myBoolValue 为假):

<button aria-controls="t1" aria-expanded="myBoolValue">Show</button>
<div *ngIf="myBoolValue" id="t1">Some text</div>

As result, when validating, I get an error: Error: The aria-controls attribute must point to an element in the same document.结果,在验证时,我得到一个错误:错误:aria-controls 属性必须指向同一文档中的一个元素。 which is clear: no id="t1" is in the DOM.很明显:DOM 中没有 id="t1"。

I should use some technique to make it different.我应该使用一些技巧让它与众不同。 As far as I understand, there might be several options (please correct):据我了解,可能有几种选择(请更正):

Option 1 (to use an additional div as wrapper which always is in the DOM):选项 1(使用额外的 div 作为包装器,它总是在 DOM 中):

<button aria-controls="t1" aria-expanded="myBoolValue">Show</button>
<div id="t1"><div *ngIf="myBoolValue">Some text</div></div>

Option 2 (display: none; instead of removing from DOM):选项 2(显示:无;而不是从 DOM 中删除):

<button aria-controls="t1" aria-expanded="myBoolValue">Show</button>
<div *ngClass="{hidden: !myBoolValue}" id="t1">Some text</div>

where .hidden {display: none;}其中.hidden {display: none;}

Option 3 (no aria-control - no errors):选项 3(无 aria-control - 无错误):

<button aria-expanded="myBoolValue">Show</button>
<div *ngIf="myBoolValue">Some text</div>

Are Option 1, 2, 3 good enough?选项 1、2、3 是否足够好? Is Option 3 valid at all?选项 3 完全有效吗? Is it an acceptable approach in case if Option 1 and Option 2 can't be easily implemented?如果选项 1 和选项 2 无法轻松实施,这是一种可接受的方法吗?

It's better to have the aria-controls attribute and have it always pointing to an existing element if you can keep it that way.最好拥有 aria-controls 属性并让它始终指向现有元素(如果可以的话)。 But all of your solutions are acceptable, even your #3.但是你所有的解决方案都是可以接受的,即使是你的#3。

Currently, assistive tools don't actually use aria-controls a lot.目前,辅助工具实际上并没有大量使用 aria-controls。 Some of them provide shortcut to quickly switch from controlling to controlled element and back, but that's almost all.其中一些提供了从控制元素快速切换到受控元素并返回的快捷方式,但仅此而已。 Therefore it's not a big loss if you must remove it completely as in your solution #3.因此,如果您必须像解决方案 #3 中那样完全删除它,这并不是什么大损失。

However, we can never imagine what aria-controls may be used for in the future, and so, solutions #1 and #2 would certainly be better than #3 at long term, if you can do it.但是,我们永远无法想象 aria-controls 将来会用来做什么,因此,如果可以的话,从长远来看,解决方案 #1 和 #2 肯定会比 #3 更好。 Just in case.以防万一。

Solution #2 is maybe slightly better than #1 because you are sure to don't break anything.解决方案 #2 可能比 #1 稍微好一点,因为你肯定不会破坏任何东西。 It could have an importance if the controlled element is interactive and focusable.如果受控元素是交互式和可聚焦的,它可能很重要。 Focusing the parent instead wouldn't make sense and it could cause subtle focus issues afterwards.相反,让父母集中注意力是没有意义的,之后可能会导致微妙的焦点问题。 IN the other hand, you are guaranteed by the standard that display:none always means never rendered at all, whatever the kind of rendering it could be (screen, speech, braille, or whatever hasn't yet been invented).另一方面,display:none 总是意味着根本不渲染,无论渲染的类型是什么(屏幕、语音、盲文或任何尚未发明的)。

So, by order of preference, I would recommend #2, #1, #3.因此,按照优先顺序,我会推荐#2、#1、#3。

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

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