Vue:点击列表切换颜色
Posted qjuly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vue:点击列表切换颜色相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue点击列表切换颜色</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="(item, index) in items"
@click="handleClick(index)"
:class="{active: currentIndex === index}"
>
{{item}}
</li>
</ul>
</div>
<script>
const app = new Vue({
el:"#app",
data: {
items: ["随笔","文章","日记","评论"],
currentIndex: 0
},
methods: {
handleClick: function(index) {
this.currentIndex = index;
}
}
})
</script>
</body>
</html>
以上是关于Vue:点击列表切换颜色的主要内容,如果未能解决你的问题,请参考以下文章
vue中按钮使用v-bind:class动态切换颜色,两种做法