今天刚学了vue,做了一个简单的小案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
span{float:right}
li{width:200px;
list-style: none;}
</style>
</head>
<body>
<div id="app">
<input type="text" v-model="val">
<button @click="add">添加</button>
<div>
<ul>
<li v-for="(item,index) in find(val)">{{item}}<span @click="remove(index)">X</span></li>
</ul>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: "#app",
data: {
arr: ['奔驰', '宝马'],
val: ''
},
methods: {
//添加
add() {
if (this.val != "")
this.arr.push(this.val);
this.val = "";
},
//查找
find(key) {
var a = this.arr;
var list = [];
for (var i = 0; i < a.length; i++) {
if (a[i].indexOf(key) !== -1) {
list.push(a[i]);
}
} return list
},
//移除
remove(index) {
this.arr.splice(index, 1)
}
},
})
</script>
</body>
</html>