Javascript實現滑鼠右鍵特色菜單,javascript滑鼠右鍵

來源:互聯網
上載者:User

Javascript實現滑鼠右鍵特色菜單,javascript滑鼠右鍵

在Web端,通常是不需要右鍵菜單,各個瀏覽器也有自己的右鍵菜單。但是對於一些特殊的網頁,是需要右鍵菜單來增加使用者體驗的,比如百度音樂,QQ郵箱,相信大家都在Web端使用過右鍵菜單了,現在來分享一下如何?,比較簡單。

運行代碼:

window.onload = function() {      document.oncontextmenu = block;      document.body.onmouseup = function(event) {        if (!event) event = window.event;        if (event.button == 2) {          var x = event.pageX || event.clientX;          var y = event.pageY || event.clientY;          document.getElementById("contextMenu").style.left = x  "px";          document.getElementById("contextMenu").style.top = y  "px";          document.getElementById("contextMenu").style.display = "block";        }      }      //阻止事件冒泡      document.getElementById("contextMenu").onclick = function(event) {        event.stopPropagation();      }      //點擊其他地方隱藏      document.onclick = function() {        document.getElementById("contextMenu").style.display = "none";      }      for (var i = 0; i < getElementsByClassName("contextMenuItem").length; i ) {         getElementsByClassName("contextMenuItem")[i].onclick = function(event) {          event = event ? event : window.event          var target = event.srcElement ? event.srcElement : event.targe;          document.getElementById("contextMenu").style.display = "none";          //alert("您點擊了: "  target.innerHTML);                   }      }     }         function block(event) {      if (window.event) {        event = window.event;        event.returnValue = false;      } else event.preventDefault();    }    //相容IE不支援getElementsByClassName    function getElementsByClassName(className, root, tagName) {      if (root) {        root = typeof root == "string" ? document.getElementById(root) : root;      } else {        root = document.body;      }      tagName = tagName || "*";      if (document.getElementsByClassName) {         return root.getElementsByClassName(className);      } else {        var tag = root.getElementsByTagName(tagName);         var tagAll = [];        for (var i = 0; i < tag.length; i ) {          for (var j = 0, n = tag[i].className.split(' '); j < n.length; j ) {            if (n[j] == className) {              tagAll.push(tag[i]);              break;            }          }        }        return tagAll;      }    }

:

以上就是本文的全部內容,希望大家可以喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.