简体   繁体   English

如何仅在jQuery中取消绑定右键单击事件

[英]How do I unbind the right click event only in jQuery

I'm dealing with a page which contains some JS that I'm unable to modify being loaded onto the page which fires off a function when an element is right clicked. 我正在处理一个页面,其中包含一些我无法修改的JS,将其加载到页面上会在右键单击某个元素时触发功能。 So, I know how to capture the right click on elements on the page, but is there a way for me to specifically unbind all right clicks and ONLY right clicks on elements? 因此,我知道如何捕获页面上元素的右键单击,但是我是否有办法专门解除所有右键单击的绑定,而仅取消对元素的右键单击? Obviously it would have been awesome if something like $(selector).unbind('rightclick) worked but it doesn't of course. 显然,如果像$(selector).unbind('rightclick)工作,那真是太棒了,但是那当然不是。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

EDIT 编辑

Some clarification...I'm not looking to prevent the default right click browser menu from appearing when the element is right clicked. 一些澄清...我不是要防止右键单击元素时出现默认的右键单击浏览器菜单。 I simply want to remove all things bound to fire off when the element is right clicked. 我只是想删除所有在右键单击该元素时必然触发的事物。 When I did this targeting all clicks, it worked fine and the default menu appeared. 当我针对所有点击进行此操作时,效果很好,并且显示了默认菜单。 The only problem then was that I also unbound all left click events, which is what I'm trying to avoid. 唯一的问题是我也取消了所有左击事件的限制,这就是我要避免的事情。

Basically, this event that I'm trying to unbind prevents the context menu from appearing at all. 基本上,我尝试解除绑定的此事件完全阻止了上下文菜单的出现。 So I'm trying to unbind that even in order to show it. 因此,即使是为了显示它,我也试图将其解除绑定。

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>jQuery disable right click easily</title>
    <script language="javascript" type="text/javascript"
      src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
    </script>
    <script type="text/javascript" language="javascript">
        $(function() {
            $(this).bind("contextmenu", function(e) {
                e.preventDefault();
            });
        }); 
    </script>
</head>
<body>
    Sed lacus. Donec lectus. Nullam pretium nibh ut turpis.
    Nam bibendum. In nulla tortor, elementum vel, tempor at,
    varius non, purus. Mauris vitae nisl nec consectetuer.
    Donec ipsum. Proin imperdiet est. Phasellus dapibus semper
    urna. Pellentesque ornare, orci in consectetuer hendrerit,
    urna elit eleifend nunc, ut consectetuer nisl felis ac diam.
    Etiam non felis. Donec ut ante. In id eros.
</body>
</html>

See reference or demo . 请参阅参考演示

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

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