web蓝桥杯真题:绝美宋词

代码及注释:

html 复制代码
<!-- 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>
javascript 复制代码
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获取数据

javascript 复制代码
axios.get(url).then(res => console.log(res))

3.for...in

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

javascript 复制代码
for (variable in object)
  statement
相关推荐
Le_ee7 分钟前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
爱上好庆祝16 分钟前
学习js的第七天(wed APIs的开始)
前端·javascript·css·学习·html·css3
KaMeidebaby1 小时前
卡梅德生物技术快报|冻干工艺开发:注射用心肌肽全流程参数优化与工程化方案
前端·其他·百度·新浪微博
ooseabiscuit1 小时前
Laravel6.x核心优化与特性全解析
android·开发语言·javascript
哆啦A梦15882 小时前
20, Springboot3+vue3实现前台轮播图和详情页的设计
javascript·数据库·spring boot·mybatis·vue3
Moment2 小时前
面试官:如果产品经理给你多个需求,怎么让AI去完成❓❓❓
前端·后端·面试
每天吃饭的羊2 小时前
JSONP
前端
gogoing2 小时前
ESLint 配置字段说明
前端·javascript
Lkstar2 小时前
面试官让我手写 Promise.all / Promise.race / Promise.allSettled,我直接水灵灵地写出来了
javascript·面试
gogoing2 小时前
CSS 属性值计算过程(Computed Value)
前端·css