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动态切换颜色,两种做法

Vue框架tab切换高亮最简易方法

VUE小练习(按钮颜色,数组映射)

如何更改 nativescript-vue 中列表项的颜色/背景颜色?

vue@2.6.14小目标列表

如何使用 Vue.js 切换按钮?