15.事件.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #box {
  9. width: 500px;
  10. height: 500px;
  11. background: #f00;
  12. }
  13. #box1 {
  14. width: 200px;
  15. height: 200px;
  16. background: #ff0;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div>
  22. <div id="box">
  23. <div id="box1"></div>
  24. </div>
  25. <input type="text">
  26. </div>
  27. <script>
  28. var box = document.getElementById("box");
  29. var inp = document.querySelector("input");
  30. inp.onkeydown = function(event) {
  31. console.log("按下",event)
  32. }
  33. inp.onkeyup = function() {
  34. console.log("抬起")
  35. }
  36. inp.onkeypress = function() {
  37. console.log("触发")
  38. }
  39. // 点击
  40. // box.onclick = function () {
  41. // console.log("box")
  42. // }
  43. // box1.onclick = function () {
  44. // console.log("box1")
  45. // // 冒泡事件
  46. // }
  47. // box.ondblclick = function () {
  48. // console.log("双击")
  49. // }
  50. box.onmousedown = function() {
  51. console.log("鼠标按下")
  52. }
  53. box.onmousemove = function() {
  54. console.log("鼠标划过")
  55. }
  56. box.onmouseup = function() {
  57. console.log("鼠标抬起")
  58. }
  59. box.onmouseout = function() {
  60. console.log("鼠标划出")
  61. }
  62. box.onmouseover = function() {
  63. console.log("当鼠标移动到某个对象范围上方时触发事件")
  64. }
  65. </script>
  66. </body>
  67. </html>