1234567891011121314151617181920212223242526272829303132 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- button{
- display: block;
- margin: 100px auto;
- width: 100px;
- height: 50px;
- }
- </style>
- </head>
- <body>
- <button>按钮</button>
- <script>
- var oBtn = document.getElementsByTagName("button")[0];
- // 鼠标左键
- oBtn.onclick = function(){
- console.log("hello world");
- }
- // 鼠标右键
- oBtn.oncontextmenu = function(){
- console.log("右键");
- // 阻止默认事件 只能写在事件函数里面
- return false;
- }
- </script>
- </body>
- </html>
|