简体   繁体   English

为什么我的event.currentTarget会自动更改?

[英]How come my event.currentTarget is changing automatically?

Please have a look at the code below. 请看下面的代码。

function deferredClick(f) {
    return (function (e) {
        var $this = $(e.currentTarget);
        console.log('Actual target: ', e.currentTarget);

        window.setTimeout(function () {
            console.log('Target I get here: ', e.currentTarget);
            f.call($this.get(0), e);
        }, 1000);
    });
}

function clickResponder(e) {
    var $this = $(e.currentTarget);
    $("#out").html('Clicked - ' + $this.val());
}

$('input[type="button"]').on('vclick', deferredClick(clickResponder));

The idea is to trigger the event handler after some fixed delay. 这个想法是在一些固定的延迟之后触发事件处理程序。 When you run the above code, you will get two logs in console. 运行上面的代码时,您将在控制台中获得两个日志。 [JSFiddle demo here - http://jsfiddle.net/D7GTP/ ] [这里的JSFiddle演示 - http://jsfiddle.net/D7GTP/ ]

Actual target: <input class="ui-btn-hidden" type="button" value="click me" data-disabled="false">
Target I get here: Document

How come e.currentTarget is mutating from line 4 to line 7? 为什么e.currentTarget从第4行变为第7行?

Please note: The event in question is vclick , which is provided by jquerymobile. 请注意:有问题的事件是vclick ,由jquerymobile提供。

How come e.currentTarget is mutating from line 4 to line 7? 为什么e.currentTarget从第4行变为第7行?

Because of event bubbling. 因为事件冒泡。 There is only one event object which is passed to all registered handlers down from the <input> up to the document . 只有一个事件对象从<input>传递到所有已注册的处理程序,直到document On every stage, the currentTarget property is changed to the current target element. 在每个阶段, currentTarget属性都更改为当前目标元素。 After the event left the document , it will be set to null . 事件离开document ,它将被设置为null

Yet, the situation is a littlebit different for you. 然而,对你而言,情况有点不同。 You've got jQuery and jQueryMobile loaded, which add their own event stuff each. 你已经加载了jQuery和jQueryMobile,它们各自添加了自己的事件。 jQuery for example constructs normalized Event objects , and it seems Mobile adds another extra layer. 例如,jQuery 构造了规范化的Event对象 ,而且似乎Mobile增加了另一个额外的层。 You can try to inspect the .originalEvent property. 您可以尝试检查.originalEvent属性。

Why is that different now? 为什么现在有所不同? The custom event construction happens on every stage, and your listener gets a unique object. 自定义事件构造发生在每个阶段,并且您的侦听器获取唯一对象。 It's currentTarget does not change. 它的currentTarget不会改变。 You can observe this when you use the normal click event. 使用普通click事件时,您可以观察到这一点。 Yet, if you use the Mobile's vclick event then event delegation will be used. 但是,如果您使用Mobile的vclick事件,则将使用事件委派。 Here, the custom event object gets reused. 这里,自定义事件对象被重用。 When it fires your handler, the currentTarget gets set to the <input> . 当它触发你的处理程序时, currentTarget被设置为<input> After that, it gets reset to the element where the delegation was bound to - the document . 之后,它将重置为委派绑定的元素 - document

When you log the properties in the timeout, you will get those from after all the modifications - not those that were relevant to you. 在超时中记录属性时,您将从所有修改后获取这些属性 - 而不是那些与您相关的修改。 The same thing happens when you console.log the event object (see lazy logging ). console.log事件对象时,会发生同样的事情(请参阅延迟日志记录 )。

TL;DR: TL; DR:

When you access the event properties during the execution of your callback, you can expect them to be accurate. 在执行回调期间访问事件属性时,可以预期它们是准确的。

When you access the event properties after the handlers were triggered, then if you're using 在触发处理程序访问事件属性时,如果您正在使用

  • plain DOM events: the currentTarget will be null plain DOM事件: currentTarget将为null
  • jQuery events: the currentTarget will stay the same jQuery事件: currentTarget将保持不变
  • jQuery delegated events (including jQueryMobile ones): the currentTarget will be the actual bound target jQuery委托事件(包括jQueryMobile): currentTarget将是实际的绑定目标

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

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