15_事件源.html 827 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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: gray;
  13. }
  14. #div2{
  15. width: 100px;
  16. height: 100px;
  17. background: aqua;
  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. div1.onclick = function(e){
  29. // console.log(this)
  30. console.log(e.target)
  31. }
  32. div2.onclick = function(e){
  33. // console.log(this)
  34. console.log(e.target)
  35. }
  36. //事件源 事件的源头
  37. </script>
  38. </body>
  39. </html>