7_DOM事件对象.html 708 B

1234567891011121314151617181920212223242526272829
  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. margin:100px auto;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="box"></div>
  18. <script>
  19. var oBox = document.getElementsByClassName("box")[0];
  20. // 参数重的e 为事件对象
  21. oBox.oncontextmenu = function(e){
  22. // 阻止浏览器默认行为
  23. e.preventDefault();
  24. console.log(e.clientY,e.clientX);
  25. }
  26. </script>
  27. </body>
  28. </html>