当前位置: 移动技术网 > IT编程>开发语言>JavaScript > vue 点击当前元素添加class 去掉兄弟的class

vue 点击当前元素添加class 去掉兄弟的class

2018年05月12日  | 移动技术网IT编程  | 我要评论

点击当前标签给其添加class,兄弟标签class删除

演示地址: https://xibushijie.github.io/static/addClass.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>vue 点击当前元素添加class 去掉兄弟的class</title>
    <script src="../js/vue.js"></script>
</head>
<style type="text/css">
    .blue {color: #2175bc;}
</style>
<body>
    <div id="app">
      <ul>
        <li v-for="(todo, index) in todos" v-on:click="addClass(index)" v-bind:class="{ blue:index==current}">
            {{ todo.text }}
        </li>
    </ul>
</div>
<script>
new Vue({
    el: '#app',
        data: {
        current:0,
            todos: [
            { text: '选项一' },
            { text: '选项二' },
            { text: '选项三' }
        ]
    },
    methods:{
        addClass:function(index){
            this.current=index;
        }
    }
})
</script>
</body>
</html>

  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网