vue中动态生成li,并且选中的li动态改变背景色

2023-12-26 11:03:15
<!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>

文章来源:https://blog.csdn.net/wangchaohpu/article/details/135214972
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。