cycold / cycold.github.io

Please dot not star...
4 stars 1 forks source link

当使用事件时, 注意事件对象event.target 与 event.currentTarget 区别 #135

Closed cycold closed 6 years ago

cycold commented 7 years ago

event.currentTarget 是添加事件句柄的元素 event.target 是第一个接收事件的元素 向上冒泡到了 event.currentTarget 的元素, 由此元素添加的句柄进行处理

所以当没有绑定this时, 就可以通过event.currentTarget 得到添加事件句柄时的元素

https://www.safaribooksonline.com/library/view/jquery-cookbook/9780596806941/ch08s09.html

http://stackoverflow.com/questions/33456789/event-target-should-be-anchor-but-is-image-instead

Use event.currentTarget. The event.target is supposed to be the img element since that is what the user has clicked on. The click then bubbles up through the image's containers. event.currentTarget gives you the element that the click handler was actually bound to.

(Or if you didn't bind this to some other object you could use this within the click handler and it should also be the current target.)