当前位置:首页 > VUE

vue实现填空

2026-03-07 02:39:42VUE

Vue 实现填空功能的方法

使用 v-model 绑定输入框

在 Vue 中,可以通过 v-model 指令实现双向数据绑定,用于填空功能的输入框。以下是一个基本示例:

<template>
  <div>
    <input v-model="answer" placeholder="请输入答案">
    <p>你的答案是:{{ answer }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      answer: ''
    }
  }
}
</script>

动态生成填空题目

如果需要动态生成填空题,可以使用 v-for 循环渲染多个填空输入框,并绑定到数组中的不同项:

<template>
  <div>
    <div v-for="(item, index) in questions" :key="index">
      <p>{{ item.text }}</p>
      <input v-model="item.answer" placeholder="填空">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      questions: [
        { text: '问题1:Vue 是一个 ___ 框架', answer: '' },
        { text: '问题2:Vue 的核心特性是 ___', answer: '' }
      ]
    }
  }
}
</script>

填空题验证

可以添加验证逻辑,检查用户是否填写了所有空:

<template>
  <div>
    <div v-for="(item, index) in questions" :key="index">
      <p>{{ item.text }}</p>
      <input v-model="item.answer" placeholder="填空">
      <span v-if="showError && !item.answer" style="color: red">请填写此空</span>
    </div>
    <button @click="checkAnswers">提交</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showError: false,
      questions: [
        { text: '问题1:Vue 是一个 ___ 框架', answer: '' },
        { text: '问题2:Vue 的核心特性是 ___', answer: '' }
      ]
    }
  },
  methods: {
    checkAnswers() {
      this.showError = true
      const allFilled = this.questions.every(item => item.answer.trim() !== '')
      if (allFilled) {
        alert('提交成功!')
      }
    }
  }
}
</script>

填空题自动评分

可以根据预设答案自动评分:

<template>
  <div>
    <div v-for="(item, index) in questions" :key="index">
      <p>{{ item.text }}</p>
      <input v-model="userAnswers[index]" placeholder="填空">
      <span v-if="showResult">
        {{ item.correctAnswer === userAnswers[index] ? '✓' : '✗' }}
      </span>
    </div>
    <button @click="showResult = true">查看结果</button>
    <p v-if="showResult">得分:{{ score }}/{{ questions.length }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showResult: false,
      userAnswers: [],
      questions: [
        { text: '问题1:Vue 是一个 ___ 框架', correctAnswer: '前端' },
        { text: '问题2:Vue 的核心特性是 ___', correctAnswer: '响应式' }
      ]
    }
  },
  computed: {
    score() {
      return this.questions.reduce((total, item, index) => {
        return total + (item.correctAnswer === this.userAnswers[index] ? 1 : 0)
      }, 0)
    }
  },
  created() {
    this.userAnswers = new Array(this.questions.length).fill('')
  }
}
</script>

填空题样式优化

可以使用 CSS 美化填空题:

vue实现填空

<template>
  <div class="fill-blank-container">
    <div v-for="(item, index) in questions" :key="index" class="question-item">
      <p>{{ item.text.replace('___', '<span class="blank"></span>') }}</p>
      <input v-model="userAnswers[index]" class="blank-input">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userAnswers: [],
      questions: [
        { text: '问题1:Vue 是一个 ___ 框架', correctAnswer: '前端' },
        { text: '问题2:Vue 的核心特性是 ___', correctAnswer: '响应式' }
      ]
    }
  },
  created() {
    this.userAnswers = new Array(this.questions.length).fill('')
  }
}
</script>

<style>
.fill-blank-container {
  max-width: 600px;
  margin: 0 auto;
}
.question-item {
  margin-bottom: 20px;
}
.blank {
  display: inline-block;
  min-width: 100px;
  border-bottom: 1px solid #333;
  margin: 0 5px;
}
.blank-input {
  border: none;
  border-bottom: 1px solid #333;
  padding: 5px;
  width: 100px;
}
</style>

这些方法可以根据实际需求组合使用,实现各种复杂的填空功能。

标签: vue
分享给朋友:

相关文章

vue实现文字播放栏

vue实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <tem…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

轮播vue实现

轮播vue实现

使用 Vue 实现轮播功能 基础轮播实现 安装依赖(如使用 Swiper): npm install swiper vue-awesome-swiper 组件代码示例: <templ…

vue实现烟花

vue实现烟花

Vue 实现烟花效果 在 Vue 中实现烟花效果可以通过结合 CSS 动画和 JavaScript 动态生成烟花元素。以下是具体实现方法: 动态生成烟花元素 通过 Vue 的 v-for 动态生成烟…