Js實現自訂右鍵行為,js實現自訂行為

來源:互聯網
上載者:User

Js實現自訂右鍵行為,js實現自訂行為

自訂右鍵行為(通過事件對象獲得滑鼠的座標(x,y))

<!doctype html><html>  <head>    <meta charset="UTF-8">    <title>demo</title>    <style type="text/css">      html{        height:100%;      }      body{        height:100%;      }      #mydiv{        width:300px;        height:300px;        background-color: #ff7400;      }      #ctxMenu{        background-color: #ff7400;        list-style-type: none;        position: absolute;        padding:0px;        border:1px solid #000;        visibility: hidden;      }    </style>  </head>  <body>    <div id="mydiv"></div>    <h2 id="show"></h2>    <input type="text"id="txt"><span id="help"></span>    <ul id="ctxMenu">      <li>上傳</li>      <li>下載</li>      <li>建立</li>      <li>取消</li>    </ul>    <script type="text/javascript">      var $=function(id){        return document.getElementById(id);      };      var h2=$("show");      var ctx=$("ctxMenu");      var txt=$("txt");      var help=$("help");      /*      txt.onfocus=function(){        help.innerHTML="請輸入金額";      }      txt.onkeydown=function(event){        help.innerHTML="";        var code=event.keyCode;        if(!(code>=48&&code<=57||code>=96&&code<=105||code==46||code==8||code==13||code==37||code==39||code==110||code==190)){          event.preventDefault();        }      }      */      document.body.oncontextmenu=function(event){        event.preventDefault();        var x=event.pageX;        var y=event.pageY;        ctx.style.left=x+"px";        ctx.style.top=y+"px";        ctx.style.visibility="visible";      }      document.body.onclick=function(){        ctx.style.visibility="hidden";      }      /*      document.body.onmousemove=function(event){        var x=event.pageX;        var y=event.pageY;        h2.innerHTML=x+":"+y;      }      */      window.onbeforeunload=function(){        var v=$("txt").value;        if(v){          return "";        }      }      $("mydiv").onclick=function(){        $("show").innerHTML="click";      };      $("mydiv").ondblclick=function(){        $("show").innerHTML="dblclick";      };      $("mydiv").onmouseover=function(){        $("show").innerHTML="mouseover";        this.style.backgroundColor="#2d2d2d";      };      $("mydiv").onmouseout=function(){        $("show").innerHTML="mouseout";        this.style.backgroundColor="pink";      }    </script>  </body></html>

以上就是本文給大家分享的全部內容了,希望大家能夠喜歡。

請您花一點時間將文章分享給您的朋友或者留下評論。我們將會由衷感謝您的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.