<!DOCTYPE html>
<html>
<head>
<title>Vue汉字模糊搜索</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="searchKeyword" placeholder="请输入搜索关键词">
<ul>
<li v-for="item in filteredData" :key="item">{{ item }}</li>
</ul>
</div>
<script>
new Vue({
el: '#app',
data: {
searchKeyword: '', // 搜索关键词
data: [
"苹果",
"香蕉",
"蓝莓",
"樱桃",
"葡萄",
"柠檬",
"橙子",
"草莓"
]
},
computed: {
filteredData() {
const keyword = this.searchKeyword.trim(); // 去除首尾空格
if (keyword) {
const regex = new RegExp(keyword, 'i'); // 不区分大小写
return this.data.filter(item => regex.test(item));
} else {
return this.data;
}
}
}
});
</script>
</body>
</html>
05-29
518
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-14
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交