语义学

表单

当创建一个表单,你可能使用到以下几个元素:<form><label><input><textarea><button>

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

  1. <form action="/dataCollectionLocation" method="post" autocomplete="on">
  2. <div v-for="item in formItems" :key="item.id" class="form-item">
  3. <label :for="item.id">{{ item.label }}: </label>
  4. <input
  5. :type="item.type"
  6. :id="item.id"
  7. :name="item.id"
  8. v-model="item.value"
  9. />
  10. </div>
  11. <button type="submit">Submit</button>
  12. </form>

注意如何在表单元素中包含 autocomplete='on',它将应用于表单中的所有输入。你也可以为每个输入设置不同的自动完成属性的值语义学 - 图1 (opens new window)

标签

提供标签以描述所有表单控件的用途;链接 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语义学 - 图3 (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 DevTools 中检查此元素,以查看可访问名称是如何更改的:

Chrome Developer Tools showing input accessible name from aria-label

aria-labelledby

使用 aria-labelledby语义学 - 图5 (opens new window) 类似于 aria-label,除非标签文本在屏幕上可见。它通过 id 与其他元素配对,你可以链接多个 id

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

Chrome Developer Tools showing input accessible name from aria-labelledby

aria-describedby

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

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

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

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

占位符

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

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

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

操作指南

为输入字段添加说明时,请确保将其正确链接到输入。你可以提供附加指令并在 aria-labelledby语义学 - 图10 (opens new window) 内绑定多个 id。这使得设计更加灵活。

  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语义学 - 图11 (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. <p id="dob-instructions">MM/DD/YYYY</p>
  6. </fieldset>

隐藏内容

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

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

  1. <form role="search">
  2. <label for="search" class="hidden-visually">Search: </label>
  3. <input type="text" name="search" id="search" v-model="search" />
  4. <button type="submit">Search</button>
  5. </form>

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

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

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

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>

功能图像

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

  • Input 字段

    • 这些图像将作为表单上的提交类型按钮
    1. <form role="search">
    2. <label for="search" class="hidden-visually">Search: </label>
    3. <input type="text" name="search" id="search" v-model="search" />
    4. <input
    5. type="image"
    6. class="btnImg"
    7. src="https://img.icons8.com/search"
    8. alt="Search"
    9. />
    10. </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>
  8. </form>