티스토리 뷰

728x90

이벤트 핸들러 내부 this binding 시 currentTarget 속성과 target 속성의 차이

 

document.body.innerHTML = '<div id="clickDiv">클릭';

document.getElementById('clickDiv').addEventListener('click', function(event){
    console.log(event.currentTarget);
    console.log(event.target);
});

 

이벤트 핸들러가 바인딩된 엘리먼트는 event.currentTarget 

 

이벤트가 실제로 발생한 엘리먼트는 event.target

 

참고

https://stackoverflow.com/questions/10086427/what-is-the-exact-difference-between-currenttarget-property-and-target-property

 

What is the exact difference between currentTarget property and target property in JavaScript

Can anyone please tell me the exact difference between currentTarget and target property in JavaScript events with example and which property is used in which scenario?

stackoverflow.com

DOC

https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget

 

Event.currentTarget - Web APIs | MDN

The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to Event.target, which identif

developer.mozilla.org

https://developer.mozilla.org/en-US/docs/Web/API/Event/target

 

Event.target - Web APIs | MDN

The read-only target property of the Event interface is a reference to the object onto which the event was dispatched. It is different from Event.currentTarget when the event handler is called during the bubbling or capturing phase of the event.

developer.mozilla.org

728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함