Javascript: Right-click a special menu and right-click javascript
On the Web end, you do not need to right-click a menu. Each browser also has its own right-click menu. However, for some special webpages, You need to right-click the menu to increase the user experience, such as Baidu music and QQ mail. I believe everyone has used the right-click menu on the Web end, it is relatively simple to share how to implement it.
Run the Code:
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" ;}}// block event bubbling document. getElementById ("contextMenu "). onclick = function (event) {event. st OpPropagation ();} // click somewhere else to hide the 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.tar ge; document. getElementById ("contextMenu "). style. display = "none"; // alert ("You clicked:" target. innerHTML) ;}} function block (event) {if (window. event) {event = window. event; event. returnValue = false;} else event. preventDefault ();} // compatible with IE does not support 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 ;}}
:
The above is all the content of this article, and I hope you will like it.