Is right click a Javascript event?

后端 未结 18 2121
南笙
南笙 2020-11-22 10:02

Is right click a Javascript event? If so, how do I use it?

18条回答
  •  不思量自难忘°
    2020-11-22 10:16

    That is the easiest way to fire it, and it works on all browsers except application webviews like ( CefSharp Chromium etc ... ). I hope my code will help you and good luck!

    const contentToRightClick=document.querySelector("div#contentToRightClick");
    
    //const contentToRightClick=window; //If you want to add it into the whole document
    
    contentToRightClick.oncontextmenu=function(e){
      e=(e||window.event);
      e.preventDefault();
      console.log(e);
      
      return false; //Remove it if you want to keep the default contextmenu 
    }
    div#contentToRightClick{
      background-color: #eee;
      border: 1px solid rgba(0,0,0,.2);
      overflow: hidden;
      padding: 20px;
      height: 150px;
    }
    Right click on the box !

提交回复
热议问题