简体   繁体   English

在特定元素上禁用移动长按上下文菜单

[英]Disable mobile longpress context menu on specific elements

I have an image gallery with various controls.我有一个带有各种控件的图片库。 One of the controls is a basic delete function, to delete you click and hold for approx 1 second to get a confirmation asking if you want to delete.其中一个控件是基本的删除功能,要删除,请单击并按住约 1 秒钟以确认询问您是否要删除。 All works fine, it's just that on mobile devices it often causes the "Save Image As, etc" menu to pop up which has to be closed before the intended action can be performed.一切正常,只是在移动设备上它经常导致“将图像另存为等”菜单弹出,必须在执行预期操作之前将其关闭。

I've read about various fixes but none of them seem to work with current versions of Chrome mobile on my Galaxy S5, and the most recent answer I could find was from 2013.我已经阅读了各种修复程序,但似乎没有一个适用于我的 Galaxy S5 上当前版本的 Chrome 移动版,我能找到的最新答案是 2013 年的。

I found one saying that the context menu was it's own function, so I tried something like this:我发现有人说上下文菜单是它自己的功能,所以我尝试了这样的事情:

    window.oncontextmenu = function(event) {
        event.preventDefault();
        event.stopPropagation();
        return false;
    };

But it did not prevent the context menu from showing on my S5.但这并没有阻止上下文菜单显示在我的 S5 上。 As I said, I'm hoping to find a solution to prevent it from coming up on certain items, not the entire window.正如我所说,我希望找到一种解决方案来防止它出现在某些项目上,而不是整个窗口。

Thanks to Tasos for the answer感谢 Tasos 的回答

document.getElementById('yourElement').oncontextmenu = function(event) {
    event.preventDefault();
    event.stopPropagation(); // not necessary in my case, could leave in case stopImmediateProp isn't available? 
    event.stopImmediatePropagation();
    return false;
};

I (re)post the answer here because at first, I haven't seen it was in the question :)我(重新)在这里发布答案,因为起初,我没有看到它在问题中:)

So juste use this code, with stopImmediatePropagation :所以juste使用这个代码,与 stopImmediatePropagation :

document.getElementById('yourElement').oncontextmenu = function(event) {
    event.preventDefault();
    event.stopPropagation(); // not necessary in my case, could leave in case stopImmediateProp isn't available? 
    event.stopImmediatePropagation();
    return false;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM