vue獲取當前對對象


 <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>vue 點擊當前元素添加class 去掉兄弟的class</title>
    <script src="../js/v ue.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> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM