我不明白有什么区别,它们看起来是一样的,但我想它们不是。
任何关于何时使用其中一个或另一个的例子都将是非常感谢的。
我不明白有什么区别,它们看起来是一样的,但我想它们不是。
任何关于何时使用其中一个或另一个的例子都将是非常感谢的。
当前回答
target is the element that triggered the event (e.g., the user clicked on)
currenttarget is the element that the event listener is attached to.
其他回答
e.currentTarget是注册事件的元素(父),e.target是事件指向的节点(子)。
我喜欢直观的答案。
当您单击#btn时,将调用两个事件处理程序,它们将输出您在图片中看到的内容。
演示在这里:https://jsfiddle.net/ujhe1key/
E.target是元素,你可以点击它 currenttarget是添加了事件监听器的元素。
如果你点击按钮的子元素,最好使用currentTarget来检测按钮属性,在CH中使用e.target有时会有问题。
target is the element that triggered the event (e.g., the user clicked on)
currenttarget is the element that the event listener is attached to.
那件事值得注意。Target可能很有用,例如,用于使用单个侦听器来触发不同的操作。假设你有一个典型的“菜单”精灵,里面有10个按钮,所以不要这样做:
menu.button1.addEventListener(MouseEvent.CLICK, doAction1);
menu.button2.addEventListener(MouseEvent.CLICK, doAction2);
etc...
你可以简单地做:
menu.addEventListener(MouseEvent.CLICK, doAction);
并根据事件在doAction(event)中触发不同的操作。目标(使用它的name属性,等等…)