javascript _target&currentTarget

장봄·2020년 5월 25일
0

code-states_4주차

목록 보기
2/13
post-thumbnail

[target&currentTarget]

자바스크립트의 이벤트 중 클릭한 요소를 가져오는 방법

event.target

  • 이벤트버블링의 가장 마지막에 위치한 최하위의 요소를 반환

event.currentTarget

  • 이벤트 생성 위치
<div onclick="a(event)" id="circle" style="width:100px; height:100px; border:1px solid red;">
   <a id="btn">[버튼]</a>
</div>
function a(){
   console.log(
     e.currentTarget.tagName == "DIV", e.target.tagName == "A"
   ); // true, true
}
profile
즐겁게 배우고 꾸준히 블로깅하는 개발자입니다 ;>

0개의 댓글