Home > Web Front-end > JS Tutorial > Introduction to the difference between event.currentTarget and event.target_javascript skills

Introduction to the difference between event.currentTarget and event.target_javascript skills

WBOY
Release: 2016-05-16 17:44:46
Original
1333 people have browsed it

event.currentTarget identifies the current target for the event, as the event traverses the DOM. It always refers to the element the event handler has been attached to as opposed to event.target which identifies the element on which the event occurred.
That is, event.currentTarget points to the element to which the event is bound, while event.target always points to the element when the event occurs. The translation is unprofessional and hard to pronounce. Let’s go directly to the test code:

Copy the code The code is as follows:



<script> <br>$('#wrapper').click(function(e ) { <br>console.log('#wrapper'); <br>console.log(e.currentTarget); <br>console.log(e.target); <br>}); <br>$( '#inner').click(function(e) { <br>console.log('#inner'); <br>console.log(e.currentTarget); <br>console.log(e.target); <br>}); <br>/* <br>The above test output is as follows: <br>When click here! click will bubble up, and the output is as follows: <br>#inner <br><a href= ​"#" id="inner">​click here!​</a>​ <br><a href=​"#" id=​"inner">​click here!​< /a>​ <br>#wrapper <br><div id=​"wrapper">​…​</div>​ <br><a href=​"#" id=​"inner" >​click here!​</a>​ <br>When you click click here!, the click will bubble up, and the output is as follows: <br>#wrapper <br><div id=​"wrapper"> ​…​</div>​ <br><div id=​"wrapper">​…​</div>​ <br>*/ <br></script>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template