16_事件流.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. *{
  10. margin: 0;
  11. padding: 0;
  12. }
  13. #div1{
  14. width: 300px;
  15. height: 300px;
  16. background: greenyellow;
  17. }
  18. #div2{
  19. width: 200px;
  20. height: 200px;
  21. background: red;
  22. }
  23. #div3{
  24. width: 100px;
  25. height: 100px;
  26. background: orange;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="div1">
  32. <div id="div2">
  33. <div id="div3"></div>
  34. </div>
  35. </div>
  36. <script>
  37. /*
  38. js的事件流 从触发事件到处理事件的整个流程
  39. 包括事件捕获和事件冒泡
  40. */
  41. var div1 = document.getElementById('div1')
  42. var div2 = document.getElementById('div2')
  43. var div3 = document.getElementById('div3')
  44. div1.addEventListener('click',function(){
  45. console.log(111)
  46. },true)
  47. div2.addEventListener('click',function(){
  48. console.log(222)
  49. },true)
  50. // div3.addEventListener('click',function(){
  51. // console.log(333)
  52. // },true)
  53. div3.onclick = function(){
  54. console.log(333)
  55. }
  56. </script>
  57. </body>
  58. </html>