点击时无法解除鼠标离开 [英] can't unbind mouse leave when clicking

查看:161
本文介绍了点击时无法解除鼠标离开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jquery脚本,其中如果你mouseenter和元素,一些东西显示,并消失时,你mouseleave。我试图解除绑定mouseleave当用户点击,以便东西,当用户离开时保持显示。这是正确的方法吗?

I have a jquery script, where if you mouseenter and element, something shows, and disappears when you mouseleave. I'm trying to unbind the mouseleave when user clicks, so that "something" stays showing when user leaves. Is this the correct way to do it? If so, can someone help me get this script to work?

谢谢!

$('.block').live("mouseenter",function(){
        var id= $(this).attr('id');
        $('#arrowPreview'+id).show();


    }).live("mouseleave",function(){
        var id= $(this).attr('id');
        $('#arrowPreview'+id).hide();

    }).live("click",function(){
        var id= $(this).attr('id');
        $('#arrowPreview'+id).show();
        $(this).unbind("mouseleave");
    });

谢谢!

推荐答案

所以我不认为你想要的是完全可能的。问题似乎是使用 .live 和解除绑定。您可以使用 .die 解除mouseleave事件。但是,使用的选择器必须匹配最初用于绑定事件的选择器,在您的情况下 .block 。我认为这显然是坏的。 .die示例小提琴

So I don't think what you want is exactly possible. The problem appears to be the use of .live and unbind. You can unbind the mouseleave event with .die. However, the selector used must match the one originally used to bind the event, in your case .block. I am thinking that this obviously is bad. Example Fiddle of .die

http://jsfiddle.net/ EZNDg /

我认为,你需要使用显式选择器绑定当前元素,所以也许使用 .each ,然后用$(this)绑定。这应该允许模具工作。我会混淆这个小提琴,看看是否是真的。

I think that instead you need to bind using an explicit selector for the current element, so perhaps using .each with your selector and then binding with $(this). This should allow die to work. I will mess with this Fiddle and see if it is true.

这篇关于点击时无法解除鼠标离开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆