vue获取当前对对象

 <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>vue 点击当前元素添加class 去掉兄弟的class</title>
    <script src="../js/vue.js"></script>
</head>
<style type="text/css">
    ul li {cursor: pointer;}
    .blue {color: #2175bc;}
</style>
<body>
    <div id="app">
      <ul>
        <li v-for="(todo, index) in todos" v-on:click="addClass(index,$event)" 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,event){
            this.current=index;
             
       //获取点击对象     
           var el = event.currentTarget;
           alert("当前对象的内容:"+el.innerHTML);
        }
    }
})
</script>
</body>
<html> 
posted @ 2019-04-13 10:47  de玻璃  阅读(4358)  评论(0编辑  收藏  举报