首页 > 代码库 > event.currentTarget与event.target
event.currentTarget与event.target
无意中在一段源码里看到event.currentTarget这个属性,这个与熟悉的event.target还是有所差别的。
event.target是指事件发生的源,在哪个元素上发生的。event.currentTarget 就是通过addEventListener绑定的DOM元素。事件是可以通过冒泡进行传递的。通过冒泡触发的事件event.target 跟event.currentTarget是不相同的元素。
引用网上的一段英文:Event objects also have target properties that reference the actual object which dispatched the event. In some cases, the target may not be the object for which you have registered a listener. This can occur when the object for which you have registered a listener contains a child component that also dispatches the same event (and the event bubbles). If you want to ensure that you are getting a reference to the object for which the listener is registered to listen for the event, use the currentTarget property。
event.currentTarget与event.target