Javascript: Right-click a special menu and right-click javascript

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.