18.事件源.html 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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: 700px;
  10. height: 700px;
  11. background: #f00;
  12. }
  13. #box2 {
  14. width: 350px;
  15. height: 350px;
  16. background: #ff0;
  17. }
  18. #box3 {
  19. width: 175px;
  20. height: 175px;
  21. background: #0f0;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="box1">
  27. <div id="box2">eee</div>
  28. <input type="text">
  29. </div>
  30. <script>
  31. var box1 = document.getElementById("box1")
  32. var box2 = document.getElementById("box2")
  33. var inp = document.querySelector("input")
  34. box1.onclick = function(event) {
  35. console.log(event);
  36. }
  37. box2.onclick = function(event) {
  38. console.log(event.target);
  39. }
  40. inp.onkeydown = function(event) {
  41. console.log(event.target.value);
  42. }
  43. </script>
  44. </body>
  45. </html>