事件监听.html 860 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. #box1 {
  9. width: 400px;
  10. height: 400px;
  11. background: #00f;
  12. }
  13. #box2 {
  14. width: 200px;
  15. height: 200px;
  16. background: #ff0;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="box1">
  22. <div id="box2"></div>
  23. </div>
  24. <script>
  25. const box1 = document.getElementById("box1");
  26. const box2 = document.getElementById("box2");
  27. box1.addEventListener('click', function () {
  28. console.log("大盒子")
  29. },false)
  30. box2.addEventListener('click', function () {
  31. console.log("小盒子")
  32. },false)
  33. </script>
  34. </body>
  35. </html>