仅在鼠标左键后显示元素一次

Show element once only after mouse left

本文关键字:一次 元素 显示 鼠标      更新时间:2023-09-26

stackoverflow!

每个人都知道好人mouseout和好人mouseleave

我决定扩展我的功能,并写了这样的东西:

    $("div").click(function(){
    $(this).on('mouseleave', function(){
            alert("Hello, bro");
        });
});

我想要什么?好吧,我点击元素,做一些事情,当我离开jQuery时说:"你好,兄弟!">

但是!

如果我关闭对话框,然后用光标返回到我可爱的元素,那么狂野的对话框将再次出现。但它应该只在单击元素后显示。

你们有什么建议吗?

$('div').click(function(){ //Setup the click
    $(this).one('mouseleave', function(){ //Use the 'one' style rather than @Bala's method
        alert('Hello, bro');
    });
});

我认为当您单击div时,必须触发鼠标离开事件。再次单击它也会激发两次,因此它将被绑定。。如果你时断时续,它就会开火一次。并且不要使用直接的div元素。你必须使用任何选择器,如id,类

$("div").click(function(){
    $(this).off('mouseleave');
    $(this).on('mouseleave', function(){
        alert("Hello, bro");
        $(this).off('mouseleave');
    });
});