小码哥的IT人生

首页 > JS > vue

Vue 3.0 语义学 实例详解

vue 2022-05-07 17:07:42小码哥的IT人生shichen

#表单

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

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

<form action="/dataCollectionLocation" method="post" autocomplete="on">
  <div v-for="item in formItems" :key="item.id" class="form-item">
    <label :for="item.id">{{ item.label }}: </label>
    <input
      :type="item.type"
      :id="item.id"
      :name="item.id"
      v-model="item.value"
    />
  </div>
  <button type="submit">Submit</button>
</form>

完整实例:

<template>
  <div id="app" class="demo">
    <h1>Simple Form</h1>
    <form action="/dataCollectionLocation" method="post" autocomplete='on'>
      <div v-for="item in formItems" :key="item.id" class='form-item'>
        <label :for="item.id">{{ item.label }}: </label>
        <input :type="item.type" :id="item.id" :name="item.id" v-model="item.value">
      </div>
      <button type="submit">Submit</button>
    </form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      formItems: [
        {
          label: 'First Name',
          id: 'first-name',
          type: 'text',
          value: ''
        },
        {
          label: 'Last Name',
          id: 'last-name',
          type: 'text',
          value: ''
        },
        {
          label: 'Email',
          id: 'email',
          type: 'email',
          value: ''
        },
      ]
    };
  }
};
</script>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
.form-item {
  margin: 1em auto;
}
.form-item label {
  display: block;
}
button {
  font-size: 1em;
}
</style>

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

#标签

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

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

试一试

完整实例:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 3.0 语义学</title>
<script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
<style>
</style>
</head>
<body>
<script>
<template>
  <form class="demo" action="/dataCollectionLocation" method="post" autocomplete="on">
    <div class="form-item">
      <label for="name">Name:</label>
      <input type="text" name="name" id="name" v-model="name" />
    </div>
    <button type="submit">Submit</button>
  </form>
</template>
<script>
export default {
  data() {
    return {
      name: ""
    };
  }
};
</script>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
.form-item {
  margin: 1em auto;
}
.form-item label {
  display: block;
}
button {
  font-size: 1em;
}
</style>
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

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

警告:

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

<label>
  Name:
  <input type="text" name="name" id="name" v-model="name" />
</label>

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

#aria-label

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

<label for="name">Name</label>
<input
  type="text"
  name="name"
  id="name"
  v-model="name"
  :aria-label="nameLabel"
/>

试一试

完整实例:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 3.0 语义学</title>
<script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
<style>
</style>
</head>
<body>
<script>
<template>
  <form class="demo" action="/dataCollectionLocation" method="post" autocomplete="on">
    <div class="form-item">
      <label for="name">Name:</label>
      <input type="text" name="name" id="name" v-model="name" :aria-label="nameLabel"/>
    </div>
    <button type="submit">Submit</button>
  </form>
</template>
<script>
export default {
  data() {
    return {
      name: "",
      nameLabel: "This label will take over the accessible name"
    };
  }
};
</script>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
.form-item {
  margin: 1em auto;
}
.form-item label {
  display: block;
}
button {
  font-size: 1em;
}
</style>
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

请随意在 Chrome DevTools 中检查此元素,以查看可访问名称是如何更改的:

#aria-labelledby

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

<form
  class="demo"
  action="/dataCollectionLocation"
  method="post"
  autocomplete="on"
>
  <h1 id="billing">Billing</h1>
  <div class="form-item">
    <label for="name">Name:</label>
    <input
      type="text"
      name="name"
      id="name"
      v-model="name"
      aria-labelledby="billing name"
    />
  </div>
  <button type="submit">Submit</button>
</form>

试一试

完整实例:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 3.0 语义学</title>
<script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
<style>
</style>
</head>
<body>
<script>
<template>
  <form class="demo" action="/dataCollectionLocation" method="post" autocomplete="on">
    <h1 id="billing">Billing</h1>
    <div class="form-item">
      <label for="name">Name:</label>
      <input type="text" name="name" id="name" v-model="name" aria-labelledby="billing name"/>
    </div>
    <button type="submit">Submit</button>
  </form>
</template>
<script>
export default {
  data() {
    return {
      name: ""
    };
  }
};
</script>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
.form-item {
  margin: 1em auto;
}
.form-item label {
  display: block;
}
button {
  font-size: 1em;
}
#billing {
    margin: 0;
}
</style>
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

#aria-describedby

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

<form
  class="demo"
  action="/dataCollectionLocation"
  method="post"
  autocomplete="on"
>
  <h1 id="billing">Billing</h1>
  <div class="form-item">
    <label for="name">Full Name:</label>
    <input
      type="text"
      name="name"
      id="name"
      v-model="name"
      aria-labelledby="billing name"
      aria-describedby="nameDescription"
    />
    <p id="nameDescription">Please provide first and last name.</p>
  </div>
  <button type="submit">Submit</button>
</form>

试一试

完整实例:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 3.0 语义学</title>
<script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
<style>
</style>
</head>
<body>
<script>
<template>
  <form class="demo" action="/dataCollectionLocation" method="post" autocomplete="on">
    <h1 id="billing">Billing</h1>
    <div class="form-item">
      <label for="name">Full Name:</label>
      <input type="text" name="name" id="name" v-model="name" aria-labelledby="billing name" aria-describedby="nameDescription"/>
      <p id="nameDescription">Please provide first and last name.</p>
    </div>
    <button type="submit">Submit</button>
  </form>
</template>
<script>
export default {
  data() {
    return {
      name: ""
    };
  }
};
</script>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
.form-item {
  margin: 1em auto;
}
.form-item label {
  display: block;
}
button {
  font-size: 1em;
}
#billing {
    margin: 0;
}
</style>
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

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

#占位符

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

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

试一试

完整实例:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 3.0 语义学</title>
<script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
<style>
</style>
</head>
<body>
<script>
<template>
  <form class="demo" action="/dataCollectionLocation" method="post" autocomplete="on">
    <div v-for="item in formItems" :key="item.id" class='form-item'>
      <label :for="item.id">{{ item.label }}: </label>
        <input type="text" :id="item.id" :name="item.id" v-model="item.value" :placeholder="item.placeholder">
    </div>
    <button type="submit">Submit</button>
  </form>
</template>
<script>
export default {
  data() {
    return {
        formItems: [
            {
                label: "First Name",
                id: "firstName",
                value: "",
                placeholder: "Evan"
            },
            {
                label: "Last Name",
                id: "lastName",
                value: "",
                placeholder: "You"
            }
        ]
    };
  }
};
</script>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
.form-item {
  margin: 1em auto;
}
.form-item label {
  display: block;
}
button {
  font-size: 1em;
}
/* https://www.www.phpcodeweb.com/howto/howto_css_placeholder.asp */
#lastName::placeholder { /* Chrome, Firefox, Opera, Safari 10.1+ */
  color: black;
  opacity: 1; /* Firefox */
}
#lastName:-ms-input-placeholder { /* Internet Explorer 10-11 */
  color: black;
}
#lastName::-ms-input-placeholder { /* Microsoft Edge */
  color: black;
}
</style>
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

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

#操作指南

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

<fieldset>
  <legend>Using aria-labelledby</legend>
  <label id="date-label" for="date">Current Date:</label>
  <input
    type="date"
    name="date"
    id="date"
    aria-labelledby="date-label date-instructions"
  />
  <p id="date-instructions">MM/DD/YYYY</p>
</fieldset>

或者,你可以用 aria-describedby将指令附加到输入。

<fieldset>
  <legend>Using aria-describedby</legend>
  <label id="dob" for="dob">Date of Birth:</label>
  <input type="date" name="dob" id="dob" aria-describedby="dob-instructions" />
  <p id="dob-instructions">MM/DD/YYYY</p>
</fieldset>

试一试

完整实例:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 3.0 语义学</title>
<script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
<style>
</style>
</head>
<body>
<script>
<template>
  <div>
    <form class="demo" action="/dataCollectionLocation" method="post" autocomplete="on">
      <fieldset>
        <legend>Using aria-labelledby</legend>
        <label id="date-label" for="date">Current Date:</label>
        <input type="date" name="date" id="date" aria-labelledby="date-label date-instructions" />
        <p id="date-instructions">MM/DD/YYYY</p>
      </fieldset>
      <fieldset>
        <legend>Using aria-describedby</legend>
        <label id="dob" for="dob">Date of Birth:</label>
        <input type="date" name="dob" id="dob" aria-describedby="dob-instructions" />
        <p id="dob-instructions">MM/DD/YYYY</p>
      </fieldset>
    </form>
  </div>
</template>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
.form-item {
  margin: 1em auto;
}
.form-item label {
  display: block;
}
button {
  font-size: 1em;
}
fieldset {
  margin: 1em auto;
}
</style>
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

#隐藏内容

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

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

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

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

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

.hidden-visually {
  position: absolute;
  overflow: hidden;
  white-space: nowrap;
  margin: 0;
  padding: 0;
  height: 1px;
  width: 1px;
  clip: rect(0 0 0 0);
  clip-path: inset(100%);
}

试一试

完整实例:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 3.0 语义学</title>
<script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
<style>
</style>
</head>
<body>
<script>
<template>
  <form class="demo" role="search" action="/dataCollectionLocation" method="post">
    <label for="search" class="hidden-visually">Search: </label>
    <input type="text" name="search" id="search" v-model="search" />
    <button type="submit">Search</button>
  </form>
</template>
<script>
export default {
  data() {
    return {
      search: ""
    };
  }
};
</script>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
.hidden-visually {
  position: absolute;
  overflow: hidden;
  white-space: nowrap;
  margin: 0;
  padding: 0;
  height: 1px;
  width: 1px;
  clip: rect(0 0 0 0);
  clip-path: inset(100%);
}
button {
  font-size: 1em;
}
</style>
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

#aria-hidden="true"

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

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

#按钮

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

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

<form action="/dataCollectionLocation" method="post" autocomplete="on">
  <!-- Buttons -->
  <button type="button">Cancel</button>
  <button type="submit">Submit</button>
  <!-- Input buttons -->
  <input type="button" value="Cancel" />
  <input type="submit" value="Submit" />
</form>

试一试

完整实例:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 3.0 语义学</title>
<script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
<style>
</style>
</head>
<body>
<script>
<template>
  <form class="demo" action="/dataCollectionLocation" method="post" autocomplete="on">
    <h1>Buttons</h1>
    <div class="form-item">
      <label for="name">Name:</label>
      <input type="text" name="name" id="name" v-model="name" />
    </div>
    <!-- Buttons -->
    <div class="btnTypes">
      <h2>Buttons</h2>
      <button type="button">Cancel</button>
      <button type="submit">Submit</button>
    </div>
    <!-- Input buttons -->
    <div class="btnTypes">
      <h2>Input Buttons</h2>
      <input type="button" value="Cancel">
      <input type="submit" value="Submit">
    </div>
  </form>
</template>
<script>
export default {
  data() {
    return {
      name: ""
    };
  }
};
</script>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
.form-item {
  margin: 1em auto;
}
.form-item label {
  display: block;
}
button {
  font-size: 1em;
}
.btnTypes {
  margin: 2em auto;
}
</style>
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

#功能图像

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

  1. Input 字段
  1. 这些图像将作为表单上的提交类型按钮
  <form role="search">
    <label for="search" class="hidden-visually">Search: </label>
    <input type="text" name="search" id="search" v-model="search" />
    <input
      type="image"
      class="btnImg"
      src="https://img.icons8.com/search" rel="external nofollow"
      alt="Search"
    />
  </form>
  1. 图标
<form role="search">
  <label for="searchIcon" class="hidden-visually">Search: </label>
  <input type="text" name="searchIcon" id="searchIcon" v-model="searchIcon" />
  <button type="submit">
    <i class="fas fa-search" aria-hidden="true"></i>
    <span class="hidden-visually">Search</span>
  </button>
</form>

试一试

完整实例:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue 3.0 语义学</title>
<script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
<style>
</style>
</head>
<body>
<script>
<template>
  <div class="demo">
    <h1> Functional Images </h1>
    <form role="search">
      <label for="search" class="hidden-visually">Search: </label>
      <input type="text" name="search" id="search" v-model="search">
      <input type="image" class="btnImg" src="https://img.icons8.com/search" alt="Search">
    </form>
    <form role="search">
      <label for="searchIcon" class="hidden-visually">Search: </label>
      <input type="text" name="searchIcon" id="searchIcon" v-model="searchIcon">
      <button type="submit">
        <i class="fas fa-search" aria-hidden="true"></i>
        <span class="hidden-visually">Search</span>
      </button>
    </form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      search: "",
      searchIcon: ""
    };
  }
};
</script>
<style>
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
* {
  font-size: 1em;
}
.hidden-visually {
  position: absolute;
  overflow: hidden;
  white-space: nowrap;
  margin: 0;
  padding: 0;
  height: 1px;
  width: 1px;
  clip: rect(0 0 0 0);
  clip-path: inset(100%);
}
.btnImg{
  height: 1em
}
form[role=search] {
  margin: 2em 0;
}
</style>
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024