浏览器上下文菜单自定义?
有没有办法在文本区域上覆盖浏览器右键上下文菜单中的“撤消”和“全选”?
谢谢。
Is there a way to override the "undo" and "select all" in right click context menu of the browser over textarea?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法编辑浏览器的内置上下文菜单,但可以使用
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 thewindow
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.我知道你可以通过注册 click() 事件来阻止整个上下文菜单打开,执行一些跨浏览器的繁琐操作来获取哪个按钮被单击,然后如果单击了正确的按钮则返回 false。
但是,我认为不可能修改上下文菜单本身,至少不能使用 JavaScript。
我应该补充一点,您可能需要重新思考为什么要这样做。这永远不会成为对任何事情的保护(有些人试图阻止从他们的网站复制图像),因为它可能只是通过关闭 JavaScript 来禁用。
更新:好的,所以你不想阻止用户做事,让他们按照你的方式做事。然后,我想最好的做法是:
您在另一条评论中提到您无法复制复制/粘贴,这是正确的,但您可以实现自己的剪贴板(这只适用于您的网络应用程序)。如果你真的有必要的话。
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 :
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.