Created
March 13, 2018 18:36
-
-
Save fabriciofmsilva/8387c24ea32bb2dbcdde230aadcddca6 to your computer and use it in GitHub Desktop.
Browser events: bubbling, capturing, and delegation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body> | |
<p> | |
<a><span>Hello</span></a> | |
</p> | |
<script> | |
const spanEl = document.querySelector('span'); | |
spanEl.addEventListener('click', handler, false); | |
const aEl = document.querySelector('a'); | |
aEl.addEventListener('click', handler, false); | |
const bodyEl = document.querySelector('body'); | |
bodyEl.addEventListener('click', handler, false); | |
window.addEventListener('click', handler, false); | |
function handler(event) { | |
console.log(this); | |
console.log(event.target); | |
console.log(event.currentTarget); | |
} | |
// jQuery event delegation sample | |
// $('body').on('click', 'a', handler) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment