简体   繁体   English

如何在 HTML 中复杂的嵌套表单结构上构建 aria/accessibility?

[英]How to structure aria/accessibility on complex nested form structure in HTML?

I have a somewhat complex "nested" form structure, which I'm doing sort of like this:我有一个有点复杂的“嵌套”表单结构,我正在做这样的事情:

 <form> <h2>Word Dictionary Entry</h2> <div class='field'> <label for="nameInput">Word</label> <input id="nameInput" name="word" /> </div> <div class='field-group-container'> <label>Meanings</label> <div class='field-group'> <label>Meaning 1</label> <div class='field'> <label for="meaning1_sense1">Sense 1.1</label> <input id="meaning1_sense1" /> </div> <div class='field'> <label for="meaning1_sense2">Sense 1.2</label> <input id="meaning1_sense2" /> </div> <button>Add sense</button> </div> <div class='field-group'> <label>Meaning 2</label> <div class='field'> <label for="meaning2_sense1">Sense 2.1</label> <input id="meaning2_sense1" /> </div> <div class='field'> <label for="meaning2_sense2">Sense 2.2</label> <input id="meaning2_sense2" /> </div> <button>Add sense</button> </div> <button>Add meaning</button> </div> </form>

Basically it is a form for like creating a Google dictionary entry, such as this one:基本上,它是一种用于创建 Google 字典条目的表单,例如:

在此处输入图像描述

This is not an image of a form, but the output you could generate from such a form.这不是表单的图像,而是您可以从这种表单生成的输出。

For each 1 , 2 , 3 , ..., those are "meaning items".对于每个1 , 2 , 3 , ...,这些都是“有意义的项目”。 Then for each meaning item, there are multiple "senses", which are slight variations on the base meaning.然后对于每个意义项,有多个“意义”,它们是基本意义的细微变化。 So in this image, for the term/word "process", there are 3 meanings , the first of which has 5 senses , and the other two meanings only have 1 sense each .所以在这张图片中,对于术语/单词“过程”,有3 个含义,第一个含义有5 个含义,而其他两个含义每个含义只有 1 个含义

  • meanings意义
    • meaning 1意义 1
      • sense 1.1感觉 1.1
      • sense 1.2感觉 1.2
      • sense 1.3感觉 1.3
      • sense 1.4感觉 1.4
      • sense 1.5感觉 1.5
    • meaning 2意思 2
      • sense 2.1意义 2.1
    • meaning 3意义 3
      • sense 3.1意义 3.1

I could go one layer deeper than that even, and say that a sense has multiple associated "concepts", but I won't take it that far for this post.我甚至可以更深入一层,并说一种感觉有多个相关的“概念”,但我不会在这篇文章中说得那么远。 Just know that the nesting isn't limited to 3 levels.只知道嵌套不限于 3 级。

The question is, how should you structure the form so it is accessible?问题是,您应该如何构建表单以使其易于访问? The higher-level "labels" probably shouldn't be labels, or should they?更高级别的“标签”可能不应该是标签,还是应该? Where should I actually use the label tag in this scenario, and what should I use from aria-roles to properly annotate this nesting?在这种情况下我应该在哪里实际使用label标签,我应该从 aria-roles 使用什么来正确注释这个嵌套?

To structure forms, the <fieldset> element along with a <legend> should be used.要构造表单, 应使用<fieldset>元素<legend>

This element has a group role , which will not add this structure to the document outline, and screen readers will announce when focus enters or leaves the group, so it's not read all the time.该元素具有组角色,不会将该结构添加到文档大纲中,屏幕阅读器会在焦点进入或离开组时进行通知,因此不会一直阅读。

 <form> <h2>Word Dictionary Entry</h2> <div class='field'> <label for="nameInput">Word</label> <input id="nameInput" name="word" /> </div> <fieldset class='field-group-container'> <legend>Meanings</legend> <fieldset class='field-group'> <legend>Meaning 1</legend> <div class='field'> <label for="meaning1_sense1">Sense 1.1</label> <input id="meaning1_sense1" /> </div> <div class='field'> <label for="meaning1_sense2">Sense 1.2</label> <input id="meaning1_sense2" /> </div> <button>Add sense</button> </fieldset> <fieldset class='field-group'> <legend>Meaning 2</legend> <div class='field'> <label for="meaning2_sense1">Sense 2.1</label> <input id="meaning2_sense1" /> </div> <div class='field'> <label for="meaning2_sense2">Sense 2.2</label> <input id="meaning2_sense2" /> </div> <button>Add sense</button> </fieldset> <button>Add meaning</button> </fieldset> </form>

There is another question to answer: How do you tell screen reader users that their new item has been added?还有一个问题需要回答:您如何告诉屏幕阅读器用户他们的新项目已添加?

One solution might be to focus the new input directly, which will announce the new element and the new group it is in.一种解决方案可能是直接关注新输入,这将宣布新元素和它所在的新组。

addMeaning.addEventListener('click', () => {
  // add new fieldset…
  // focus the first input in the new fieldset
  newFieldset.querySelector('input').focus();
});

How one would go about deleting a meaning or sense is not clear from your question.您的问题尚不清楚如何删除含义或感觉。

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

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