在 Android 上长按时禁用上下文菜单
我想禁用在网络应用程序中长时间点击(触摸并按住)图像后出现的上下文菜单。我看过关于如何做到这一点的不同想法的帖子,但它们似乎都不适合我。
有没有办法通过 HTML/CSS/Javascript 在 Android 上做到这一点?
I would like to disable the context menu that appears after a long tap (touch and hold) on images in my web application. I've seen posts with different ideas how to do it, but none of them seem to work for me.
Is there a way to do this on Android via HTML/CSS/Javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
上下文菜单有自己的事件。你只需要抓住它并阻止它传播。
The context menu has its own event. You just need to catch it and stop it from propagating.
这应该适用于 1.6 或更高版本(如果我没记错的话)。我不认为 1.5 或更早版本有解决方法。
This should work on 1.6 or later (if I recall correctly). I don't believe there's a workaround for 1.5 or earlier.
这可以使用 CSS 来完成:
That can be done using CSS:
对我来说,吸收所有事件不是一个选择,因为我想禁用长按下载,同时仍然允许用户缩放和平移图像。我只能用 css 和 html 解决这个问题,只需在图像顶部放置一个“shield”div,如下所示:
希望这对某人有帮助!
For me, absorbing all the events was not an option since I wanted to disable long press downloads while still allowing the user to zoom and pan on the image. I was able to solve this with css and html only by layering a "shield" div on top of the image like so:
Hope this helps someone!
它将禁用复制,但不会禁用选择。
在 webView 中工作。
It will disable copy, but do not disable selection.
Works in webView.
我使用 Nurik 的完整示例,但该元素(我页面中的输入图像)也无法单击。
我通过以下方式更改原始行:
原始行:
我的更改:
使用此方法,我禁用 logpress 上的弹出保存图像菜单,但保留单击事件。
我正在运行 Android 2.2 的 7 英寸平板电脑上使用 Dolphin HD 浏览器进行测试,运行良好!
I use the complete example by Nurik but the the element (an input image in my page) was disable for the click too.
I change the original line by this:
original line:
my change:
with this approuch i disable the pop-up save image menu on logpress but keep the click event.
I´m testing on a 7" tablet with Android 2.2 under a Dolphin HD browser and works fine!
使用此 CSS 代码用于移动设备
Use this CSS codes for mobile
我也有类似的问题。我已经在这个线程和另一个线程中尝试了几个解决方案来解决同一问题(阻止移动 Safari (iPad / iPhone) 中长按/长按时出现默认上下文菜单)。不好的部分是我无法使用 onTouchStart、onTouchEnd 等...
只能阻止 onContextMenu 的事件。 React 16.5.2 的片段。
仅在 Chrome 中进行了测试。
希望它对某人有帮助。干杯!
I've had a similar issue. I've tried couple of solution from this thread and another thread for safari on the same problem (Preventing default context menu on longpress / longclick in mobile Safari (iPad / iPhone)) . The bad part was that I couldn't use onTouchStart,onTouchEnd etc...
Only prevent the event from onContextMenu. Snippet from React 16.5.2.
Tested in chrome only.
Hope it helps somebody. Cheers!
捕获 onContextMenu 事件,并在事件处理程序中返回 false。
无论如何,在某些浏览器中,您还可以捕获单击事件并使用 event.button 检查哪个鼠标按钮触发了该事件。
Capture the onContextMenu event, and return false in the event handler.
You can also capture the click event and check which mouse button fired the event with event.button, in some browsers anyway.
刚刚有类似的问题。上述建议在 Andoid 浏览器中对我不起作用,但我发现将有问题的图像显示为 CSS 背景图像而不是嵌入图像可以解决问题。
Just had a similar problem. The above suggestions did not work for me in the Andoid browser, but I found that displaying the problematic image as a CSS background image rather than an embedded image fixed the problem.
通过原始 JavaScript,不会为上下文菜单调用任何事件。也许在 Java 世界里有一些东西......在 Android webkit 中实际上存在一些关于 javascript 事件的问题(例如焦点无法正常工作)。
Through raw javascript there are no events that get called for the context menu. Perhaps in the Java world there is something... There are actually several issues regarding javascript events (such as focus not working right) in the Android webkit.