Javascript _ javascript tips for calling context menus

Source: Internet
Author: User
This article mainly introduces examples of js context menu calling, which has a certain reference value. Interested friends can refer to the examples in this article to describe the example code of js context menu calling, we will share this with you for your reference. The details are as follows:

Principle
A contextmenu event is triggered when you right-click it. By default, the default context menu of the browser is triggered. By manually blocking this default action, the custom context menu is displayed, hide this menu when you click it.
Code
1. html

  • Button 1
  • Button 2
  • Button 3

2. js

// Add the contextmenu event var right = document. getElementById ("right"); EventUtil. addEventListener (right, "contextmenu", function (event) {event = EventUtil. getEvent (event); EventUtil. preventDefault (event); var menu = document. getElementById ("menu"); // obtain the coordinates when you right-click the mouse, and set the context menu Position page = EventUtil. getPagePosition (event); menu. style. left = page. pageX + "px"; menu. style. top = page. pageY + "px"; menu. style. visibility = "visible" ;}); // Add EventUtil to hide the context menu event. addEventListener (document, "click", function (event) {var menu = document. getElementById ("menu"); menu. style. visibility = "hidden ";});

The EventUtil code appears in this article:How to Use js cross-browser Event Listeners and event objects

The above is all the content in this article. We will teach you how to call the context menu in js and hope to help you learn 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.