<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>绝美宋词</title><link rel="stylesheet" href="css/style.css" /><script src="./js/vue.min.js"></script><script src="./js/axios.min.js"></script>
</head><body><div id="app"><h1 style="text-align: center">输入关键字,找一首词</h1><div class="search-form"><input type="text" id="search" class="search" placeholder="词牌名 词句 词人" v-model="keyword" /><ul class="suggestions"><li v-if="keyword.trim().length>0 && newList.length!==0" v-for="(item, index) in newList" :key="index"><span class="poet" v-html="highlight(item.poetry_content)">{{item.poetry_content}}</span><span class="title"><span v-html="highlight(item.title)"></span>-<span v-html="highlight(item.author)"></span></span></li></ul></div></div><script>let vm = new Vue({el: '#app',data() {return {keyword: '',list: [],newList: []}},async created() {const res = await axios.get('data.json')console.log(res);this.list = [...res.data]},watch: {keyword: function (val) {if (val.trim().length > 0) {console.log('keyword变化了');for (let i = 0; i < this.list.length; i++) {if (this.list[i].author.includes(val) || this.list[i].title.includes(val) || this.list[i].poetry_content.includes(val)) {this.newList.push(this.list[i])}}} else {this.newList = []}}},methods: {highlight(content) {return content.replaceAll(this.keyword, `<span class="highlight">$&</span>`)}}})</script>
</body></html>