当前位置:首页 > 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 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于截…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景。…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客户端…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…