浏览器上下文菜单自定义?

发布于 2024-08-15 09:31:39 字数 54 浏览 1 评论 0原文

有没有办法在文本区域上覆盖浏览器右键上下文菜单中的“撤消”和“全选”?

谢谢。

Is there a way to override the "undo" and "select all" in right click context menu of the browser over textarea?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-08-22 09:31:39

您无法编辑浏览器的内置上下文菜单,但可以使用 window 对象上的 oncontextmenu 事件禁用它并用您自己的上下文菜单替换它。我要提醒大家的是,这通常是一个坏主意。用户希望找到内置的上下文菜单,但当它不存在时常常会感到沮丧。

You cannot edit the browser's built-in context menu, but you can disable it and replace it with your own using the oncontextmenu event on the window object. I would caution that this is often a bad idea. Users expect to find the built-in context menu and are often frustrated when it isn't there.

挽手叙旧 2024-08-22 09:31:39

我知道你可以通过注册 click() 事件来阻止整个上下文菜单打开,执行一些跨浏览器的繁琐操作来获取哪个按钮被单击,然后如果单击了正确的按钮则返回 false。

但是,我认为不可能修改上下文菜单本身,至少不能使用 JavaScript。

我应该补充一点,您可能需要重新思考为什么要这样做。这永远不会成为对任何事情的保护(有些人试图阻止从他们的网站复制图像),因为它可能只是通过关闭 JavaScript 来禁用。

更新:好的,所以你不想阻止用户做事,让他们按照你的方式做事。然后,我想最好的做法是:

  1. 为用户提供一个工具栏,允许他们执行这些操作(从而使他们使用您的操作而不是默认操作
  2. 将常用的键盘快捷键映射到您的操作(Ctrl+A、Ctrl +Z等...)
  3. 将右键菜单替换为您自己的菜单。

您在另一条评论中提到您无法复制复制/粘贴,这是正确的,但您可以实现自己的剪贴板(这只适用于您的网络应用程序)。如果你真的有必要的话。

I know you can prevent the whole context menu from opening by registering to the click() event, doing some cross-browser mumbo-jumbo to get wich button was clicked, and then return false if the right one was clicked.

However, I don't think it's possible to modify the context menu itself, at least not using javascript.

I should add that you may want to rethink why you're doing this. This will never be a protection against anything (some try to prevent copying images from their website), as it may simply be disabled by turning javascript off.

UPDATE: Ok, so you don't want to prevent users to do things, bug have them doing things your way. Then, I guess the best thing to do is :

  1. Provide users with a toolbar that allow them to do these things (and thus making them use your actions instead of the default one
  2. Map the usual keyboard shortcuts to your actions (Ctrl+A, Ctrl+Z, etc...)
  3. Replace the right click menu with your own.

You mentionned in another comment that you cannot reproduce copy/paste, which is correct, but you can implement you own clipboard (that will only work for your webapp) if you really have to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文