vue的li点击事件如何绑定类名?

template代码是

<div class="news_title" v-on:click="news_title" >
    <ul>
        <li class="news_active" type1="0">篮球</li>
        <li type1="1">中超</li>
        <li type1="2">英超</li>
        <li type1="3">西甲</li>
        <li type1="4">德甲</li>
        <li type1="5">意甲</li>
        <li type1="6">幸运球场</li>                 
    </ul>
</div>

原jq代码是:

$(".news_title").on("click",'li',function(){
    $(".news_title li").removeClass("news_active");
    $(this).addClass("news_active");
    var type=$(this).attr("type1");
});

请教下怎么迁移这个jq到vue上做点击事件呢?即点击li添加类名,兄弟去除类名.
看网上的方法都是要做for循环,请问下有不做for循环就能实现的方法吗?前提不要太复杂,蟹蟹

阅读 6.3k
2 个回答
<li v-for="(item, index) in list" :class="{ 'active': activeIndex === index}" @click="activate(index)">篮球</li>

data(){
    return {
        activeIndex: 0
    }
},
methods: {
    activate(index){
        this.activeIndex = index
    }
}

vue有v-class可以自动绑定css

<li v-class="{'你的css class": active}'>

active是你需要改变的值,true/false

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题