Event.target,Event.toElement和Event.srcElement有什么区别? [英] What is the difference between Event.target, Event.toElement and Event.srcElement?

查看:539
本文介绍了Event.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,div .foo

div.foo, div.foo, div.foo

点击< input> 上的鼠标右键,返回:

By clicking the right mouse button on a <input>, returns this:


输入,输入,输入

input, input, input

所有人似乎都带来了同样的结果。是否存在其中一个与其他用户有不同用途的情况?

All seem to bring the same result. Is there any situation that one of them has different use than the others?

推荐答案

事件目标是调度事件的元素:


活动使用 DOM事件
进行定位。事件目标是 Event.target <的值/ code>
属性。

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 是获取目标的IE非标准方式

当前事件目标是具有当前调用的事件侦听器的元素:

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


在事件流中,当前事件目标是与$ a bref相关联的对象,其中事件处理程序。这个
对象可以是事件目标本身或者一个它的祖先。
事件从$ b传播时,当前事件目标会发生变化$ b通过事件流的各种阶段反对。
当前事件目标是
的值 Event.currentTarget 属性。

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.

某些类型的事件具有 relatedTarget

Some kind events have a relatedTarget:


用于识别次要 EventTarget
,具体取决于事件的类型。

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

fromElement toElement 是IE的非标准方式获取 relatedTarget

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

这篇关于Event.target,Event.toElement和Event.srcElement有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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