如何将自定义右键单击菜单添加到网页? [英] How to add a custom right-click menu to a webpage?
问题描述
我想在我的网络应用程序中添加自定义右键单击菜单。这可以在不使用任何预先构建的库的情况下完成吗?如果是这样,如何显示一个不使用第三方JavaScript库的简单自定义右键单击菜单?
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?
我的目标是Google Docs的功能。它允许用户右键单击并向用户显示他们自己的菜单。
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.
推荐答案
回答你的问题 - 使用 contextmenu
事件,如下所示:
Answering your question - use contextmenu
event, like below:
<html>
<head>
<script type="text/javascript">
if (document.addEventListener) { // IE >= 9; other browsers
document.addEventListener('contextmenu', function(e) {
alert("You've tried to open context menu"); //here you draw your own menu
e.preventDefault();
}, false);
} else { // IE < 9
document.attachEvent('oncontextmenu', function() {
alert("You've tried to open context menu");
window.event.returnValue = false;
});
}
</script>
</head>
<body>
Lorem ipsum...
</body>
</html>
但是你应该问问自己,你真的想要覆盖默认的右键单击行为 - 它取决于你正在开发的应用程序。
But you should ask yourself, do you really want to overwrite default right-click behavior - it depends on application that you're developing.
这篇关于如何将自定义右键单击菜单添加到网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!