16_事件流.html 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #div1 {
  10. width: 300px;
  11. height: 300px;
  12. background: red;
  13. }
  14. #div2 {
  15. width: 200px;
  16. height: 200px;
  17. background: green;
  18. }
  19. #div3 {
  20. width: 100px;
  21. height: 100px;
  22. background: pink;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="div1">
  28. <div id="div2">
  29. <div id="div3"></div>
  30. </div>
  31. </div>
  32. <script>
  33. /* js的事件流 从触发事件到处理时间的整个流程 包括事件捕获和事件冒泡 */
  34. var div1 = document.getElementById('div1')
  35. var div2 = document.getElementById('div2')
  36. var div3 = document.getElementById('div3')
  37. div1.addEventListener('click',function(){
  38. console.log('div1')
  39. },true)
  40. div2.addEventListener('click',function(){
  41. console.log('div2')
  42. },true)
  43. // div3.addEventListener('click',function(){
  44. // console.log('div3')
  45. // },true)
  46. div3.onclick = function(){
  47. console.log('div3')
  48. }
  49. </script>
  50. </body>
  51. </html>