Target、 Event.toElement 和 Event.srcElement 之间的区别是什么?

我有以下密码:

document.oncontextmenu = function(evt) {
evt = evt || window.event;
console.log(evt.target, evt.toElement, evt.srcElement);
};

通过单击 <div class="foo"></div>上的鼠标右键,返回以下内容:

Div,foo,div,foo

通过单击 <input>上的鼠标右键,返回以下内容:

输入,输入,输入

所有这些似乎都带来了同样的结果。有没有可能其中一个和其他的有不同的用途?

45661 次浏览

The event target is the element to which the event is dispatched:

The object to which an event is targeted using the DOM event flow. The event target is the value of the Event.target attribute.

srcElement is a IE non-standard way to obtain the target.

The current event target is the element which has the event listener which is currently invoked:

In an event flow, the current event target is the object associated with the event handler that is currently being dispatched. This object MAY be the event target itself or one of its ancestors. The current event target changes as the event propagates from object to object through the various phases of the event flow. The current event target is the value of the Event.currentTarget attribute.

Using this inside an event listener is a common (and standard) way to obtain the current event target.

Some kind events have a relatedTarget:

Used to identify a secondary EventTarget related to a UI event, depending on the type of event.

fromElement and toElement are IE non-standard ways to obtain the relatedTarget.