阻止默认时间
demo:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .box{width:200px;height: 300px;background: #ccc;position: absolute;left:0;top:0;display: none} .box ul{margin: 0} </style> </head> <body> <!-- <a href="http://www.baidu.com">21321312</a> --> <div class="box"> <ul> <li>选项1</li> <li>选项2</li> <li>选项3</li> <li>选项4</li> </ul> </div> </body> <script> var obox = document.querySelector(".box") document.oncontextmenu = function(eve){ //oncontextmenu 事件在元素中用户右击鼠标时触发并打开上下文菜单。 var e = eve || window.event; obox.style.display = "block"; // return false; // 取消默认事件 stopDefault(e); obox.style.left = e.offsetX + "px"; obox.style.top = e.offsetY + "px"; } document.onclick = function(){ obox.style.display = "none" } function stopDefault(e){ if(e.preventDefault){ e.preventDefault() //IE }else{ e.returnValue = false; //GOOGLE } } //封装函数 </script> </html>
长风破浪会有时,直挂云帆济沧海