5_DOM事件.html 791 B

1234567891011121314151617181920212223242526272829303132
  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. button{
  9. display: block;
  10. margin: 100px auto;
  11. width: 100px;
  12. height: 50px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <button>按钮</button>
  18. <script>
  19. var oBtn = document.getElementsByTagName("button")[0];
  20. // 鼠标左键
  21. oBtn.onclick = function(){
  22. console.log("hello world");
  23. }
  24. // 鼠标右键
  25. oBtn.oncontextmenu = function(){
  26. console.log("右键");
  27. // 阻止默认事件 只能写在事件函数里面
  28. return false;
  29. }
  30. </script>
  31. </body>
  32. </html>