简体   繁体   English

如何在字段集中正确显示错误消息

[英]How to properly show an error message inside a fieldset

I want to have a fieldset -tag to show an error message. 我想要一个fieldset -tag来显示错误消息。

When doing something like: 当做类似的事情时:

<fieldset>
  <legend>Some Legend</legend>
  <div role="alert" aria-live="assertive" aria-relevant="all">
    My Error Message
  </div>

  ...
  <!-- formfields -->
</fieldset>

The screen reader isn't reading the error message, but when wrapping the error message inside the <label> -tag it is reading it but it feels wrong. 屏幕阅读器没有读取错误消息,但是将错误消息包装在<label> -tag中时,它正在读取错误消息,但是感觉不对。

What would be the proper way to show an error message inside a fieldset -tag in terms of accessibility? 就可访问性而言,在fieldset -tag中显示错误消息的正确方法是什么?

aria-live regions work pretty well if you follow a few simple rules. 如果您遵循一些简单的规则,那么aria-live区域的效果会很好。

  • the element with aria-live must exist at page load time. 具有aria-live的元素必须在页面加载时存在。 that is, you can't dynamically add that attribute after the page is loaded. 也就是说,您无法在页面加载后动态添加该属性。
  • if using the default aria-relevant value (that is, if you don't specify aria-relevant ), then changes are only announced if you change the text in the region or if you add child DOM nodes. 如果使用默认的aria-relevant值(即,如果您未指定aria-relevant ),则仅当您更改区域中的文本或添加子DOM节点时,才会宣布更改。 In your case, you have "all" so child DOM nodes that are removed will also be announced. 在您的情况下,您拥有“全部”,因此也将宣布已删除的子DOM节点。 Note that hiding or unhiding elements is not considered a change in the DOM so will not be announced. 请注意,隐藏或取消隐藏元素不视为DOM的更改,因此不会宣布。

A few side notes: 一些注意事项:

  • in general, aria-live="polite" is sufficient for the majority of cases. 通常,在大多数情况下, aria-live="polite"就足够了。 aria-live="assertive" is rarely needed except for extremely urgent messages. 除了非常紧急的消息外,很少需要aria-live="assertive"
  • using role="alert" gives you an implicit aria-live="assertive" . 使用role="alert"给您一个隐式的aria-live="assertive" you don't have to specify aria-live . 您不必指定aria-live

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

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