简体   繁体   English

vue.js中的点击和鼠标事件冲突

[英]click and mouse event conflict in vuejs

recently,i try to write a todo demo via vuejs,however,when i write the following code 最近,我尝试通过vuejs编写todo演示,但是,当我编写以下代码时

<div id="item_lists">
                <div class='user_choice_item' v-for="todo in todos" @mouseenter="showDeleteBtn($index,$event)" v-on:mouseleave.self="hideDeleteBtn($index,$event)">
                    <input type='checkbox' name='item_cbx' v-model="todo.checked" />
                    <label class='with_cbx_item'>{{todo.content}}</label>
                    <span class='delete_bt'  v-on:click.stop="deleteTodo(todo)" v-show="todo.show"></span>
                </div>
            </div>

//js // js

var todoList = new Vue({
    el:"#item_lists",
    data:{
        todos:[]
    },
    methods:{
        showDeleteBtn:function(index,event){
            event.stopPropagation();
            if(event.currentTarget.className!=="user_choice_item")
                return;

            var newState = Object.assign({},this.todos[index],{show:true});
            this.todos.$set(index,newState);
        },
        hideDeleteBtn:function(index,event){
            var newState = Object.assign({},this.todos[index],{show:false});
            this.todos.$set(index,newState);
        },
        deleteTodo:function(todo){

            this.todos.$remove(todo);
            return false;
        },


    }
});

it turns out that the only the mouseenter event can be triggered correctly, the click on "delete_btn" and change event on the checkbox ,the mouseleave event not triggered. 事实证明,只有mouseenter事件可以正确触发,单击“ delete_btn”并在复选框上更改事件,mouseleave事件未触发。 however, when i remove the mouseenter event of the parent div. 但是,当我删除父div的mouseenter事件时。 the child events work .i wanna know what causes this ..can anyone help me? 儿童活动有效。我想知道是什么原因造成的。有人可以帮助我吗?

I think you may be working too hard at handling the event. 我认为您在处理事件方面可能会太努力。 Here's a snippet that does what I think you want it to do. 这是一个片段,可以满足您的要求。 The showDeleteBtn and hideDeleteBtn methods simply set the show data member of the todo item. showDeleteBtnhideDeleteBtn方法只是设置todoshow数据成员。

 var todoList = new Vue({ el: "#item_lists", data: { todos: [{ content: 'Hi there', checked: true, show: false }, { content: 'Another', checked: true, show: false }] }, methods: { showDeleteBtn: function(index) { this.todos[index].show = true; }, hideDeleteBtn: function(index) { this.todos[index].show = false; }, deleteTodo: function(todo) { this.todos.$remove(todo); return false; } } }); 
 .delete_bt { background-color: lightblue; padding: 0.3em 1em; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script> <div id="item_lists"> <div class='user_choice_item' v-for="todo in todos" v-on:mouseenter="showDeleteBtn($index)" v-on:mouseleave="hideDeleteBtn($index)"> <input type='checkbox' name='item_cbx' v-model="todo.checked" /> <label class='with_cbx_item'>{{todo.content}}</label> <span class='delete_bt' v-on:click.stop="deleteTodo(todo)" v-show="todo.show">Delete</span> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM