<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<style>
.changeColor {
color: #42b983;
}
</style>
</head>
<!--需求描述:点击列表中的哪一项,那么该项的文字就变色-->
<body>
<div id= 'vue' >
<h3>
<ul>
<li v-for="(famous, index) in stars" v-bind:class="{changeColor:index==currentIndex}" v-on:click=btnClick(index)>{{index}}-{{famous}}</li>
</ul>
</h3>
</div>
<script>
let vm = new Vue({
el: '#vue',
data:{
currentIndex :0,
stars: ['Lee', 'Leo', 'Lili', 'Alex'],
},
methods:{
btnClick: function (index){
this.currentIndex = index //使currentIndex=index,这样就可以控制哪一个变色,相当于和class绑定
}
},
})
</script>
</body>
</html>
网友评论