javascript - vue使用v-for渲染列表后如何改变状态?
巴扎黑
巴扎黑 2017-04-11 13:32:27
[JavaScript讨论组]

我的目的就是当点击列表的时候,列表改变相应的样式,也就是变为选中状态。
为什么我点击相应列表的时候不会做出响应呢?
但是console.log里面的数据确实改变了,求解!
可以不可以在此基础上解决数据无法响应的问题?

Vue版本: v2.0.3

html:

javascript:

var app = new Vue({
    el: '#app',
    data: {
        lists: [
            {title: '标题1'}, 
            {title: '标题2'}, 
            {title: '标题3'}, 
            {title: '标题4'}, 
            {title: '标题5'}, 
            {title: '标题6'}, 
        ],
        active: [true, false, false, false, false, false]
    },
    methods: {
        changeList: function(index) {
            var sideBarList = document.querySelectorAll('.list');
            for (let i = 0; i < sideBarList.length; i++) {
                if (sideBarList[i] != sideBarList[index]) {
                    this.active[i] = false;
                } else {
                    this.active[i] = true;
                }
            }
            console.log(this.active);//这里会有变化
        }
    }
});

css:

    ul{
        padding: 0;
        margin: 0;
        width: 300px;
    }
    .list{
        list-style: none;
        width: 300px;
    }
    a{    
        display: block;
        width: 100%;
        height: 30px;
        line-height: 30px;
        text-decoration: none;
            background-color: #eee;
    }
    a:hover, a.active{
        background-color: #999;    
        color: white;
    }
巴扎黑
巴扎黑

全部回复(1)
高洛峰

this.active[i]是不会被检测到的,不会更新dom,要使用this.$set(this.active, i, false)

数组更新检测

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号