如何向网页添加自定义右键菜单?
我想向我的 Web 应用程序添加自定义右键菜单。可以在不使用任何预构建库的情况下完成此操作吗?如果是这样,如何显示一个不使用第 3 方 JavaScript 库的简单自定义右键菜单?
我的目标是像 Google Docs 那样。它允许用户右键单击并向用户显示他们自己的菜单。
注意: 我想学习如何制作自己的东西,而不是使用别人已经制作的东西,因为大多数时候,那些第三方库的功能臃肿,而我只想要我需要的功能,所以我希望它完全由我手工制作。
I want to add a custom right-click menu to my web application. Can this be done without using any pre-built libraries? If so, how to display a simple custom right-click menu which does not use a 3rd party JavaScript library?
I'm aiming for something like what Google Docs does. It lets users right-click and show the users their own menu.
NOTE:
I want to learn how to make my own versus using something somebody made already since most of the time, those 3rd party libraries are bloated with features whereas I only want features that I need so I want it to be completely hand-made by me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
回答你的问题 - 使用
contextmenu
事件,如下所示:但您应该问自己,您真的想覆盖默认的右键单击行为吗 - 这取决于您正在开发的应用程序。
JSFIDDLE
Answering your question - use
contextmenu
event, like below:But you should ask yourself, do you really want to overwrite default right-click behavior - it depends on application that you're developing.
JSFIDDLE
对我来说非常有用。为了像我这样期待菜单绘制的人,我把我用来制作右键菜单的代码放在这里:
Was very useful for me. For the sake of people like me, expecting the drawing of menu, I put here the code I used to make the right-click menu:
一些漂亮的 CSS 和一些没有外部库的非标准 html 标签的组合可以 给出一个不错的结果 (JSFiddle)< /a>
HTML
注意:菜单标签不存在,我正在制作它(你可以使用任何东西)
CSS
JavaScript 是仅就这个示例而言,我个人将其删除以用于 Windows 上的持久菜单
另请注意,您可以修改
menu > menu{left:100%;}
到menu > menu{right:100%;}
用于从右向左展开的菜单。不过,您需要在某处添加边距或其他东西A combination of some nice CSS and some non-standard html tags with no external libraries can give a nice result (JSFiddle)
HTML
Note: the menu tag does not exist, I'm making it up (you can use anything)
CSS
The JavaScript is just for this example, I personally remove it for persistent menus on windows
Also note, you can potentially modify
menu > menu{left:100%;}
tomenu > menu{right:100%;}
for a menu that expands from right to left. You would need to add a margin or something somewhere though根据这里和其他流程的答案,我制作了一个看起来像 Google Chrome 的版本,带有 css3 过渡。
JS Fiddle
让我们开始吧,因为我们这个页面有了上面的js,我们就可以关心css和布局了。我们将使用的布局是一个带有
元素或 font Awesome 图标的
元素(
) 和
显示键盘快捷键。结构如下:
我们将它们放在一个 div 中,并在右键单击时显示该 div。让我们像 Google Chrome 中那样设置它们的样式,好吗?
现在我们将添加已接受答案中的代码,并获取光标的 X 和 Y 值。为此,我们将使用
e.clientX
和e.clientY
。我们使用的是客户端,因此菜单 div 必须固定。就是这样!只需添加 css 过渡以淡入和淡出,就完成了!
According to the answers here and on other 'flows, I've made a version that looks like the one of Google Chrome, with css3 transition.
JS Fiddle
Lets start easy, since we have the js above on this page, we can worry about the css and layout. The layout that we will be using is an
<a>
element with a<img>
element or a font awesome icon (<i class="fa fa-flag"></i>
) and a<span>
to show the keyboard shortcuts. So this is the structure:We will put these in a div and show that div on the right-click. Let's style them like in Google Chrome, shall we?
Now we will add the code from the accepted answer, and get the X and Y value of the cursor. To do this, we will use
e.clientX
ande.clientY
. We are using client, so the menu div has to be fixed.And that is it! Just add the css transisions to fade in and out, and done!
最简单的跳跃启动功能,在光标位置创建一个上下文菜单,该菜单在鼠标离开时会自行销毁。
Simplest jump start function, create a context menu at the cursor position, that destroys itself on mouse leave.
纯 JS 和 css 解决方案,用于真正动态的右键单击上下文菜单,尽管基于元素 id、链接等的预定义命名约定。
jsfiddle
您可以将代码复制粘贴到单个静态 html 页面中:
Pure JS and css solution for a truly dynamic right click context menu, albeit based on predefined naming conventions for the elements id, links etc.
jsfiddle
and the code you could copy paste into a single static html page :
您可以尝试通过将以下内容添加到正文标记来简单地阻止上下文菜单:
这将阻止对上下文菜单的所有访问(不仅从鼠标右键,还从键盘)。
PS,您可以将其添加到您想要禁用上下文菜单的任何标签,
例如:
仅在该特定 div 中禁用上下文菜单
You could try simply blocking the context menu by adding the following to your body tag:
This will block all access to the context menu (not just from the right mouse button but from the keyboard as well).
P.S. you can add this to any tag you want to disable the context menu on
for example:
Will disable the context menu in that particular div only
已在 Opera 11.6、firefox 9.01、Internet Explorer 9 和 chrome 17 中测试并运行
Tested and works in Opera 11.6, firefox 9.01, Internet Explorer 9 and chrome 17
试试这个:
Try this:
这是一个非常好的关于如何构建自定义上下文菜单的教程 带有完整的工作代码示例(没有 JQuery 和其他库)。
您还可以在 GitHub 上找到演示代码。
他们给出了详细的分步说明,您可以按照这些说明构建自己的右键单击上下文菜单(包括 html、css 和 javascript 代码),并在最后通过给出完整的示例代码进行总结。
您可以轻松地遵循并根据自己的需要进行调整。并且不需要 JQuery 或其他库。
他们的示例菜单代码如下所示:
可以在 codepen 上找到工作示例(任务列表)。
Here is a very good tutorial on how to build a custom context menu with a full working code example (without JQuery and other libraries).
You can also find their demo code on GitHub.
They give a detailed step-by-step explanation that you can follow along to build your own right-click context menu (including html, css and javascript code) and summarize it at the end by giving the complete example code.
You can follow along easily and adapt it to your own needs. And there is no need for JQuery or other libraries.
This is how their example menu code looks like:
A working example (task list) can be found on codepen.
我知道这个问题已经得到解答,但我花了一些时间研究第二个答案,以使本机上下文菜单消失并让它显示在用户单击的位置。
HTML
CSS
JavaScript
CodePen 示例
I know this has already been answered, but I spent some time wrestling with the second answer to get the native context menu to disappear and have it show up where the user clicked.
HTML
CSS
JavaScript
CodePen Example
试试这个
http://jsfiddle.net/AkshayBandivadekar/zakn7Lwb/14/
Try This
http://jsfiddle.net/AkshayBandivadekar/zakn7Lwb/14/
2023 年 12 月更新:
这是我的
Menu
类的示例,我用它来在笔记管理应用程序中的笔记上实现上下文菜单。特点:
ESC
键或滚动主窗口时关闭。onOpen
和onClose
。December 2023 update:
Here is an example of my
Menu
class which I use to implement context menu on notes in a note management application.Features:
ESC
key or scrolling the main window.onOpen
andonClose
.你可以用这段代码来做到这一点。
访问此处获取自动边缘检测的完整教程 http://www.voidtricks.com /自定义右键单击上下文菜单/
`
You can do it with this code.
visit here for full tutorial with automatic edge detection http://www.voidtricks.com/custom-right-click-context-menu/
`
一种简单的方法是使用 onContextMenu 返回 JavaScript 函数:
通过输入
return false;
您将取消上下文菜单。如果您仍想显示上下文菜单,只需删除
return false;
行即可。A simple way you could do it is use onContextMenu to return a JavaScript function:
And by entering
return false;
you will cancel out the context menu.if you still want to display the context menu you can just remove the
return false;
line.已在 Opera 12.17、firefox 30、Internet Explorer 9 和 chrome 26.0.1410.64 中测试并运行
Tested and works in Opera 12.17, firefox 30, Internet Explorer 9 and chrome 26.0.1410.64
对于那些正在寻找使用 bootstrap 5 和 jQuery 3 的自定义上下文菜单的非常简单的独立实现的人来说,这里是......
改编自 https://codepen.io/anirugu/pen/xjjxvG
For those looking for a very simple self-contained implementation of a custom context menu using bootstrap 5 and jQuery 3, here it is...
Adapted from https://codepen.io/anirugu/pen/xjjxvG
我在这里所做的
创建您自己的自定义 div 菜单并设置位置:绝对和显示:无,以防万一。
向要点击的页面或元素添加 oncontextmenu 事件。
返回 false 取消默认浏览器操作。
用户js来调用您自己的操作。
What I'm doing up here
Create your own custom div menu and set the position: absolute and display:none in case.
Add to the page or element to be clicked the oncontextmenu event.
Cancel the default browser action with return false.
User js to invoke your own actions.
您应该记住,如果您想使用仅限 Firefox 的解决方案,如果您想将其添加到整个文档中,您应该将
contextmenu="mymenu"
添加到标签不要添加到
body
标签中。你应该注意这一点。
You should remember if you want to use the Firefox only solution, if you want to add it to the whole document you should add
contextmenu="mymenu"
to the<html>
tag not to thebody
tag.You should pay attention to this.
您可以调整和修改此代码以制作更好看、更高效的上下文菜单。至于修改现有的上下文菜单,我不知道该怎么做...看看这个 小提琴以获得有组织的观点。另外,尝试单击我的上下文菜单中的项目。他们应该提醒您一些很棒的消息。如果它们不起作用,请尝试更......复杂的东西。
You can tweak and modify this code to make a better looking, more efficient contextmenu. As for modifying an existing contextmenu, I'm not sure how to do that... Check out this fiddle for an organized point of view. Also, try clicking the items in my contextmenu. They should alert you a few awesome messages. If they don't work, try something more... complex.
我使用类似于以下内容 jsfiddle
如果您的目标是较旧的 IE 浏览器,您无论如何都应该完成它与 ' AttachEvent;案件
I use something similar to the following jsfiddle
if You target older IE browsers you should anyway complete it with the ' attachEvent; case
根据 mozilla 的说法,上下文菜单已被弃用:
https://developer.mozilla.org/en-US/文档/Web/HTML/Global_attributes/contextmenu
According to mozilla context menu is deprecated:
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contextmenu