语义学

    标签通常放置在表单字段的顶部或左侧:

    注意如何在表单元素中包含 autocomplete='on',它将应用于表单中的所有输入。你也可以为每个输入设置不同的。

    提供标签以描述所有表单控件的用途;链接 forid

    1. <label for="name">Name</label>
    2. <input type="text" name="name" id="name" v-model="name" />

    如果你在 chrome 开发工具中检查这个元素,并打开 Elements 选项卡中的 Accessibility 选项卡,你将看到输入是如何从标签中获取其名称的:

    Chrome开发工具显示可从标签输入的可访问名称

    警告:

    虽然你可能已经看到这样包装输入字段的标签:

    1. <label>
    2. Name:
    3. <input type="text" name="name" id="name" v-model="name" />
    4. </label>

    辅助技术更好地支持用匹配的 id 显式设置标签。

    aria-label

    你也可以给输入一个带有aria-label (opens new window) 的可访问名称。

    1. <label for="name">Name</label>
    2. <input
    3. type="text"
    4. name="name"
    5. id="name"
    6. v-model="name"
    7. :aria-label="nameLabel"
    8. />

    Chrome Developer Tools showing input accessible name from aria-label

    aria-labelledby

    使用 类似于 aria-label,除非标签文本在屏幕上可见。它通过 id 与其他元素配对,你可以链接多个 id

    Chrome Developer Tools showing input accessible name from aria-labelledby

    aria-describedby

    aria-describedby (opens new window) 的用法与 aria-labelledby 相同,预期提供了用户可能需要的附加信息的描述。这可用于描述任何输入的标准:

    1. <form
    2. class="demo"
    3. action="/dataCollectionLocation"
    4. method="post"
    5. autocomplete="on"
    6. <div class="form-item">
    7. <label for="name">Full Name:</label>
    8. <input
    9. type="text"
    10. name="name"
    11. id="name"
    12. v-model="name"
    13. aria-labelledby="billing name"
    14. aria-describedby="nameDescription"
    15. />
    16. <p id="nameDescription">Please provide first and last name.</p>
    17. </div>
    18. <button type="submit">Submit</button>
    19. </form>

    你可以通过使用 Chrome 开发工具来查看说明:

    Chrome开发工具显示aria-labelledby的输入可访问名称和aria-describedby的描述

    避免使用占位符,因为它们可能会混淆许多用户。

    占位符的一个问题是默认情况下它们不符合;修复颜色对比度会使占位符看起来像输入字段中预填充的数据。查看以下示例,可以看到满足颜色对比度条件的姓氏占位符看起来像预填充的数据:

    最好提供用户在任何输入之外填写表单所需的所有信息。

    1. <fieldset>
    2. <legend>Using aria-labelledby</legend>
    3. <label id="date-label" for="date">Current Date:</label>
    4. <input
    5. type="date"
    6. name="date"
    7. id="date"
    8. aria-labelledby="date-label date-instructions"
    9. />
    10. <p id="date-instructions">MM/DD/YYYY</p>
    11. </fieldset>

    或者,你可以用 aria-describedby语义学 - 图5 (opens new window)将指令附加到输入。

    1. <fieldset>
    2. <legend>Using aria-describedby</legend>
    3. <label id="dob" for="dob">Date of Birth:</label>
    4. <input type="date" name="dob" id="dob" aria-describedby="dob-instructions" />
    5. </fieldset>

    通常不建议直观地隐藏标签,即使输入具有可访问的名称。但是,如果输入的功能可以与周围的内容一起理解,那么我们可以隐藏视觉标签。

    让我们看看这个搜索字段:

    我们可以这样做,因为搜索按钮将帮助可视化用户识别输入字段的用途。

    我们可以使用 CSS 直观地隐藏元素,但可以将它们用于辅助技术:

    1. position: absolute;
    2. overflow: hidden;
    3. white-space: nowrap;
    4. margin: 0;
    5. padding: 0;
    6. height: 1px;
    7. width: 1px;
    8. clip: rect(0 0 0 0);
    9. clip-path: inset(100%);
    10. }

    aria-hidden=”true”

    添加 aria hidden="true" 将隐藏辅助技术中的元素,但使其在视觉上对其他用户可用。不要把它用在可聚焦的元素上,纯粹用于装饰性的、复制的或屏幕外的内容上。

    1. <p>This is not hidden from screen readers.</p>
    2. <p aria-hidden="true">This is hidden from screen readers.</p>

    在表单中使用按钮时,必须设置类型以防止提交表单。

    也可以使用输入创建按钮:

    1. <form action="/dataCollectionLocation" method="post" autocomplete="on">
    2. <!-- Buttons -->
    3. <button type="button">Cancel</button>
    4. <button type="submit">Submit</button>
    5. <!-- Input buttons -->
    6. <input type="button" value="Cancel" />
    7. <input type="submit" value="Submit" />
    8. </form>

    功能图像

    你可以使用此技术创建功能图像。

      • 这些图像将作为表单上的提交类型按钮
    • 图标

    1. <form role="search">
    2. <label for="searchIcon" class="hidden-visually">Search: </label>
    3. <input type="text" name="searchIcon" id="searchIcon" v-model="searchIcon" />
    4. <button type="submit">
    5. <i class="fas fa-search" aria-hidden="true"></i>
    6. <span class="hidden-visually">Search</span>
    7. </button>