简体   繁体   English

如何确定某个事件是否由创建该事件的同一事件触发?

[英]How can I tell if an event is being fired by the same event that created it?

Let's say I have the following events: 假设我发生以下事件:

$(button).on('mouseup', function (event1) {
    $(something).show();

    $(document).on('mouseup', function (event2) {
        $(something).hide();
    });

});

So, it shows "something" when button is clicked and hides it when the document is clicked. 因此,当单击按钮时,它显示“某些内容”,而在单击文档时,则将其隐藏。 How can I make sure the second evnet doesn't trigger in the same event that created it? 如何确保第二个evnet在创建它的同一事件中不会触发? right now the something will show and hide instantly (at least on firefox). 现在,事物将立即显示和隐藏(至少在Firefox中)。

I need to do this without any globals of any kind, preferably. 我最好在没有任何全局变量的情况下执行此操作。

How about this: 这个怎么样:

$(button).on('mouseup', function (event1) {
    $(something).show();
    event1.stopPropagation();
    $(document).one('mouseup', function (event2) {
        $(something).hide();
    });

});

stopPropagation() will stop the event from going past the button (to the document). stopPropagation()将使事件停止通过按钮(到达文档)。

one() will only run the event once and then go away... can be recreated again with another click on the button. one()将只运行一次事件,然后消失...可以再次单击该按钮重新创建。

JSFiddle 的jsfiddle

Here's another solution that doesn't rely on timestamps: 这是另一种不依赖时间戳的解决方案:

$("button").on('mouseup', function (event1) {
    $("#something").show();

    setTimeout(function () {
        $(document).one('mouseup', function (event2) {
             $("#something").hide();
        });
    }, 0);

}); 

demo 演示

Using setTimeout with a delay of 0 will make it execute the code as soon as it's finished with this event. 使用setTimeout延迟为0将使它在完成此事件后立即执行代码。 Also note I'm using one rather than on because you only need this event handler one time and without it you will end up attaching unlimited numbers of event handlers, every single one of which will need processing when a mouseup fires anywhere on your page. 还要注意,我使用的是one而不是one on因为您只需要一次使用此事件处理程序,而没有它,您最终将附加无数个事件处理程序,当mouseup在页面上的任何位置触发时,每个事件处理程序都需要处理。

A less silly solution might look like this: 一个不太愚蠢的解决方案可能看起来像这样:

$("button").on('mouseup', function (event1) {
     $("#something").show();
});

$(document).on('mouseup', function (event2) {
    if(event2.target != $("button")[0]) {
        $("#something").hide();
    }
});

demo 演示

Why don't you isolate the event like so: 您为什么不这样隔离事件:

$(button).on('mouseup', function (event1) {
    $(something).show();
});

$(document).on('mouseup', function (event2) {
        $(something).hide();
});

I ended up using the event.timeStamp property to check if the two events are distinct. 我最终使用event.timeStamp属性检查两个事件是否不同。 Also added an unbind and a namespace to the document event to prevent event stacking. 还向文档事件添加了取消绑定和名称空间,以防止事件堆叠。

$(button).on('mouseup', function (event1) {
    $(something).show();

    $(document).on('mouseup.'+someIdentifier, function (event2) {
        if(event1.timeStamp != event2.timeStamp) {
            $(something).hide();
            $(document).unbind('mouseup.'+someIdentifier);
        }
    });

});

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

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