web蓝桥杯真题:绝美宋词

代码及注释:

<!-- TODO:待补充代码 -->
<div class="search-form">
  <input type="text" v-model="search" id="search" @input="handleInput" class="search" placeholder="词牌名 词句 词人"/>
  <ul class="suggestions" >
    <li v-for="item in showList" v-key="item.poetry_content">    //循环
      <span class="poet" v-html="highlight(item.poetry_content)"></span>  //v-html指令渲染
      <span class="title" v-html="highlight(item.title) + '-' + highlight(item.author)"></span>
    </li>
  </ul>
</div>
let vm = new Vue({
  el:'#app',
  // TODO:待补充代码
  data: {
    search: '',
    dataList: [],
    showList: []
  },
  mounted() {
    axios.get('./data.json').then(res => this.dataList = res.data)  //获取数据
  },
  methods: {
    handleInput() {
      this.showList = this.dataList.filter(item => {    //筛选含有关键字的数组
        for (const key in item) {        //循环对象,将含有关键字的对象返回
          if(item[key].includes(this.search)) {   
            return item
          }
        }
      })
      if (!this.search) {    //当关键字为0,数组为0
        this.showList = []
      }
    },
    highlight(text) {    //高光关键字
      return text.replaceAll(this.search, `<span class="highlight">${this.search}</span>`)
    }
  }
})

知识点:
1.v-html指令

它可以设置元素的 innerHTML 属性,从而实现 html 结构的解析和渲染

2.axios获取数据

axios.get(url).then(res => console.log(res))

3.for...in

该循环将迭代对象本身的所有可枚举属性

for (variable in object)
  statement

相关推荐

  1. web宋词

    2024-03-17 11:10:04       38 阅读
  2. web:展开你的扇子

    2024-03-17 11:10:04       47 阅读
  3. web:卡片话标签页

    2024-03-17 11:10:04       38 阅读
  4. web:分阵营,比高低

    2024-03-17 11:10:04       34 阅读
  5. web:搜一搜呀

    2024-03-17 11:10:04       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-17 11:10:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-17 11:10:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-17 11:10:04       87 阅读
  4. Python语言-面向对象

    2024-03-17 11:10:04       96 阅读

热门阅读

  1. 内存的分区

    2024-03-17 11:10:04       40 阅读
  2. MongoDB技术学习指南

    2024-03-17 11:10:04       38 阅读
  3. C++ filesystem库介绍

    2024-03-17 11:10:04       46 阅读
  4. C语言如何进⾏字符串的⽐较?

    2024-03-17 11:10:04       39 阅读
  5. HarmonyOS 网络请求工具库封装,直接无脑用!!!

    2024-03-17 11:10:04       40 阅读
  6. sqlplus设置提示符

    2024-03-17 11:10:04       45 阅读
  7. 3月16日,每日信息差

    2024-03-17 11:10:04       40 阅读
  8. mysql的基本知识点

    2024-03-17 11:10:04       41 阅读