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

SIS*_*SYN 5 html javascript css jquery android

我有一个带有各种控件的图片库。其中一个控件是基本的删除功能,要删除,请单击并按住约 1 秒钟以确认询问您是否要删除。一切正常,只是在移动设备上它经常导致“将图像另存为等”菜单弹出,必须在执行预期操作之前将其关闭。

我已经阅读了各种修复程序,但似乎没有一个适用于我的 Galaxy S5 上当前版本的 Chrome 移动版,我能找到的最新答案是 2013 年的。

我发现有人说上下文菜单是它自己的功能,所以我尝试了这样的事情:

    window.oncontextmenu = function(event) {
        event.preventDefault();
        event.stopPropagation();
        return false;
    };
Run Code Online (Sandbox Code Playgroud)

但这并没有阻止上下文菜单显示在我的 S5 上。正如我所说,我希望找到一种解决方案来防止它出现在某些项目上,而不是整个窗口。

感谢 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;
};
Run Code Online (Sandbox Code Playgroud)

fre*_*727 5

我(重新)在这里发布答案,因为起初,我没有看到它出现在问题中:)

所以只需使用这段代码,并带有 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;
};
Run Code Online (Sandbox Code Playgroud)