<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h1>{{message}}</h1>
<ul>
<li v-for="(item, index) in movies" v-on:click="changeRed(index)" v-bind:class="{active: isRedIndex === index}">{{item}}</li>
</ul>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '你好啊',
movies: ['海王', '星际穿越', '大话西游', '少年派', '盗梦空间'],
isRedIndex: -1,
},
methods: {
changeRed(index) {
if(this.isRedIndex === index) {
this.isRedIndex = -1;
} else {
this.isRedIndex = index;
}
}
}
})
</script>
</body>
</html>
ul,li列表,点击那个li,那个li改变颜色
最新推荐文章于 2024-08-23 17:35:50 发布