7_移动端点击事件.html 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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: 200px;
  10. height: 200px;
  11. background-color: red;
  12. }
  13. .box2{
  14. width: 100px;
  15. height: 100px;
  16. background-color: blue;
  17. position: absolute;
  18. top: 50px;
  19. left: 50px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="box"></div>
  25. <div class="box2"></div>
  26. <script>
  27. var oBox = document.querySelector(".box");
  28. var oBox2 = document.querySelector(".box2");
  29. // 点透事件 需要避免在移动端使用click事件
  30. oBox2.ontouchstart = function(){
  31. console.log("触摸事件");
  32. this.style.display= "none";
  33. }
  34. oBox.onclick =function(){
  35. console.log("点击事件");
  36. }
  37. // oBox.onclick = function(){
  38. // console.log("hello")
  39. // }
  40. // touchstart 触摸开始
  41. // oBox.ontouchstart = function(){
  42. // console.log("触摸开始");
  43. // }
  44. // // touchend 触摸结束
  45. // oBox.ontouchend = function(){
  46. // console.log("触摸结束");
  47. // }
  48. // // touchmove 触摸移动
  49. // oBox.ontouchmove = function(){
  50. // console.log("触摸移动");
  51. // }
  52. </script>
  53. </body>
  54. </html>