12.事件.html 1.8 KB

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