<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>title</title>
    <style>
        li {
            list-style: none;
            cursor: pointer;
            width: 300px;
        }

        .bg {
            background-color: red;
            color: #fff;
        }
    </style>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="(item,index) in movies" 
                @click="changeBgRed(index)" 
                :class="{bg:index == activeIndex}">{{item}}
            </li>
        </ul>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                movies: ['你的名字', '千与千寻', '萤火虫之墓', '天气之子'],
                activeIndex: 0
            },
            methods: {
                changeBgRed(index) {
                    this.activeIndex = index;
                }
            }
        })
    </script>
</body>

</html>

Logo

前往低代码交流专区

更多推荐