<!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>
.active {
color: red;
}
</style>
<script src="../js/vue.js"></script>
</head>
<body>
<div id="app">
<ul>
<!-- 先for循环遍历 movies class active:true 默认值是0 监听点击事件itemClick(索引号) -->
<li v-for="(item,index) in movies" :class="{active: index===currentIndex}" @click="itemClick(index)">{{item}}</li>
</ul>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
movies: ['海王', '海尔兄弟', '进击的巨人', '火影忍者'],
currentIndex: 0
},
methods: {
// 点击后把index传给 currentIndex
itemClick: function(index) {
this.currentIndex = index
}
}
});
</script>
</body>
</html>
Vue中的排他思想案例
最新推荐文章于 2022-12-03 22:00:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)