vue.js 实现阅读理解单选题

实现阅读理解单选题

场景:四级/六级/考研中都有阅读理解题型,我们来实现一个单选题组件,每道题只能选择一次,选择错误自动出现正确答案。

本例所用的数据结构

question: [
	{
		answer: 0,
		option: [
			'string1',
            'string2',
            'string3',
            'string4'
         ]
     },
     {
         answer: 2,
         option: [
            'string1',
            'string2',
            'string3',
            'string4'
         ]
     }
],

组件开发

前端界面

<template>
  <div class="question-container">
    <div class="question-cell_box" v-for="(q, qIndex) in question" :key="`${q.answer}-${qIndex}`">
      <p class="box-header"> {{ `第${qIndex + 1}题` }}</p>
      <ul class="box-option_container">
        <li
          class="box-option_item"
          v-for="(opt, index) in q.option"
          :key="`${opt}-${index}`"
          :ref="`${qIndex}-${index}`"
          @click="selectOption(q.answer, index, qIndex)"
        >{{ String.fromCharCode(64 + index + 1) }}. {{ opt }} </li>
      </ul>
    </div>
  </div>
</template>

样式

.question-container {
  width: 375px;
  border: 1px solid #E5E5E5;
  border-radius: 4px;
  margin: 0 auto;
}

.question-container .question-cell_box {
  background: #F5F5F5;
  padding: 0 20px 20px;
  margin: 20px;
}

.box-header {
  border-bottom: 1px solid #E5E5E5;
  line-height: 30px;
  padding-top: 8px;
}

.box-option_container {
  list-style: none;
  padding-inline-start: 0;
}

.box-option_item {
  border: 1px solid #E5E5E5;
  margin: 8px 0;
  border-radius: 4px;
  background: #FFFFFF;
  padding: 8px 12px;
  cursor: pointer;
  text-align: left;
}

.correct {
  background: #409EFF;
  color: #FFFFFF;
}

.error {
  background: #FFFFFF;
  color: red;
}

实现交互

selectOption (correct, index, qIndex) {
      // 此处加限制的目的是选过的题不能再选
      if (this.selectIndex !== '') {
        const historyArr = JSON.parse(window.localStorage.getItem('history'))
        const i = historyArr.findIndex((val) => val.qIndex === qIndex)
        if (i !== -1) {
          return false
        }
      }
      this.selectIndex = index
      this.history.push({
        qIndex: qIndex,
        answer: correct
      })
      window.localStorage.setItem('history', JSON.stringify(this.history))
      const optionItem = this.$refs[`${qIndex}-${index}`][0]
      if (correct === index) {
        optionItem.className = optionItem.className.concat(' correct')
      }
      if (correct !== index) {
        const correctItem = this.$refs[`${qIndex}-${correct}`][0]
        correctItem.className = correctItem.className.concat(' correct')
        optionItem.className = optionItem.className.concat(' error')
      }
    }

效果截图

在这里插入图片描述
未选择状态

在这里插入图片描述
第1题选择正确则直接显示正确选项样式,第2题选择错误显示正确答案,并用红色标红样式来标记错误答案。

如果大佬们有更好的方法,欢迎评论区探讨哦~~

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值