Created
February 7, 2018 02:28
-
-
Save HuanxinHu/9213c1e33dc4b9e70398237c0ea54477 to your computer and use it in GitHub Desktop.
JS Bin event bubbling and capture // source http://jsbin.com/jayucob
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="event bubbling and capture"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<style id="jsbin-css"> | |
.grandChild{ | |
border: 1px solid red; | |
} | |
.child{ | |
width: 50px; | |
padding: 16px; | |
border: 1px solid green; | |
} | |
.father{ | |
width: 90px; | |
padding: 4px; | |
border: 1px solid; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="father"> | |
DIV | |
<p class="child"> | |
P <span class="grandChild">SPAN</span></p> | |
</div> | |
<script id="jsbin-javascript"> | |
document.querySelector('.grandChild').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('capture span') | |
}, true) | |
document.querySelector('.child').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('capture p') | |
}, true) | |
document.querySelector('.father').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('capture div') | |
}, true) | |
document.querySelector('.grandChild').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('bubbling span') | |
}) | |
document.querySelector('.child').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('bubbling p') | |
}) | |
document.querySelector('.father').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('bubbling div') | |
}) | |
</script> | |
<script id="jsbin-source-css" type="text/css">.grandChild{ | |
border: 1px solid red; | |
} | |
.child{ | |
width: 50px; | |
padding: 16px; | |
border: 1px solid green; | |
} | |
.father{ | |
width: 90px; | |
padding: 4px; | |
border: 1px solid; | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">document.querySelector('.grandChild').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('capture span') | |
}, true) | |
document.querySelector('.child').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('capture p') | |
}, true) | |
document.querySelector('.father').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('capture div') | |
}, true) | |
document.querySelector('.grandChild').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('bubbling span') | |
}) | |
document.querySelector('.child').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('bubbling p') | |
}) | |
document.querySelector('.father').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('bubbling div') | |
}) | |
</script></body> | |
</html> |
This file contains 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
.grandChild{ | |
border: 1px solid red; | |
} | |
.child{ | |
width: 50px; | |
padding: 16px; | |
border: 1px solid green; | |
} | |
.father{ | |
width: 90px; | |
padding: 4px; | |
border: 1px solid; | |
} |
This file contains 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
document.querySelector('.grandChild').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('capture span') | |
}, true) | |
document.querySelector('.child').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('capture p') | |
}, true) | |
document.querySelector('.father').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('capture div') | |
}, true) | |
document.querySelector('.grandChild').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('bubbling span') | |
}) | |
document.querySelector('.child').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('bubbling p') | |
}) | |
document.querySelector('.father').addEventListener('click', function(evt){ | |
console.log(evt.target.classList) | |
alert('bubbling div') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment