举报投诉联系我们 手机版 热门标签 VUE中文网
您的位置:VUE中文网 > vue语义化 Vue 3.0 语义学

vue语义化 Vue 3.0 语义学

2023-02-25 06:17 VUE3教程

vue语义化 Vue 3.0 语义学

vue语义化

Vue语义化是一种把组件的功能和用途表达出来的方式,它可以帮助开发者更好地理解组件的功能,并且可以更快地实现需求。

Vue语义化的核心思想是将组件的功能和用途表达出来,使得开发者可以更好地理解组件的功能,并且可以更快地实现需求。例如:一个表单元素应该有一个名字(name)、一个标题(title)、一个值(value)、一个占位文本(placeholderText)、一个错误信息文本(errorText) 等。这些属性都是有意义的名字而不是随意命名的。

Vue语义化还可以帮助开发者避免重复代码。例如当你想要创建多个相似的表单元素时,你就不需要重复写相同的代码了。你只需要使用相同的属性来创建多个表单元素就可以了。

// 使用 Vue 语义化创建表单元素
const formElements = [{ 
    name: 'name', 
    title: 'Name', 
    value: '', 
    placeholderText: 'Please enter your name', 
    errorText: 'Name is required' 
}, { 
    name: 'email', 
    title: 'Email', 
    value: '', 
    placeholderText: 'Please enter your email address', 
    errorText: 'Email is required'   }];  

 // 初始化表单元素   formElements.forEach(element => {     // 初始化代码 });   

#表单

当创建一个表单,你可能使用到以下几个元素:<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>

点击此处实现

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

#标签

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

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

点击此处实现

如果你在 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"
/>

点击此处实现

请随意在 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>

点击此处实现

#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>

点击此处实现

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

#占位符

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

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

点击此处实现

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

#操作指南

为输入字段添加说明时,请确保将其正确链接到输入。你可以提供附加指令并在 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>

点击此处实现

#隐藏内容

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

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

<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%);
}

点击此处实现

#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>

点击此处实现

#功能图像

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

  • Input 字段

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

  <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>

  • 图标

<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>

点击此处实现

阅读全文
以上是VUE中文网为你收集整理的vue语义化 Vue 3.0 语义学全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
  • vue3.0ui Vue 3.0 标准

    vue3.0ui Vue 3.0 标准

    2023-02-26 VUE3教程

    万维网联盟 (W3C) 网络可访问性倡议 (WAI) 为不同的组件制定了 Web 可访问性标准:用户代理无障碍指南 (UAAG)网络浏览器和媒体播...

  • vue3.0实战 Vue 3.0 介绍

    vue3.0实战 Vue 3.0 介绍

    2023-02-23 VUE3教程

    INFO刚接触 Vue.js?先从基础指南开始吧。本指南主要是为有 Vue 2 经验的用户希望了解 Vue 3 的新功能和更改而提供的。在试用 Vu...

  • vue中ref的用法 Vue 3.0 v-for中的Ref数组

    vue中ref的用法 Vue 3.0 v-for中的Ref数组

    2023-02-28 VUE3教程

    在 Vue 2 中,在 v-for 里使用的 ref attribute 会用 ref 数组填充相应的 $refs property。当存在嵌套的 v-for 时,这种行为会变...

  •  Vue 3.0 attribute强制行为

    Vue 3.0 attribute强制行为

    2023-03-01 VUE3教程

    信息这是一个低级的内部 API 更改,不会影响大多数开发人员。#概览下面是对这些变化的高层次总结:删除枚举 attribute 的内部概...

  • vue过滤器的使用 Vue 3.0 过滤器

    vue过滤器的使用 Vue 3.0 过滤器

    2023-02-23 VUE3教程

    #概览从 Vue 3.0 开始,过滤器已删除,不再支持。#2.x 语法在 2.x,开发者可以使用过滤器来处理通用文本格式。例如:templateh1B...

© 2024 VUE中文网 vue88.com 版权所有 联系我们