17_事件流.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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: 200px;
  11. height: 200px;
  12. background: aqua;
  13. }
  14. #div2 {
  15. width: 100px;
  16. height: 100px;
  17. background: antiquewhite
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="div1">
  23. <div id="div2"></div>
  24. </div>
  25. <script>
  26. var div1 = document.getElementById('div1')
  27. var div2 = document.getElementById('div2')
  28. // div2.onclick = function(){
  29. // console.log('我是div2')
  30. // }
  31. // div1.onclick = function(){
  32. // console.log('我是div1')
  33. // }
  34. //事件捕获
  35. //元素.addEventListener('事件',函数,布尔值)
  36. div1.addEventListener('click', function () {
  37. console.log('我是div1')
  38. }, true)
  39. div2.addEventListener('click', function () {
  40. console.log('我是div2')
  41. }, true)
  42. </script>
  43. </body>
  44. </html>