jQuery 如何使用jquery禁用html页面中的右键菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23948383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:25:14  来源:igfitidea点击:

how to disable right click menu in html page using jquery

jqueryhtml

提问by M.J.Ahmadi

I am looking for a simple way to hide/disable the right click context menu for the whole html page but not in some editable html elements just like input[text] and textarea using jquery.

我正在寻找一种简单的方法来隐藏/禁用整个 html 页面的右键单击上下文菜单,但不是在一些可编辑的 html 元素中,就像使用 jquery 的 input[text] 和 textarea。

I know this jquery code, but the below code will disable context menu in all html elements even in editable objects...

我知道这个 jquery 代码,但下面的代码将禁用所有 html 元素中的上下文菜单,即使在可编辑对象中...

$(document).ready(function(){
    $(this).bind("contextmenu", function(e) {
        e.preventDefault();
    });
});

回答by reyaner

you could check for tags that you need like this:

你可以像这样检查你需要的标签:

$(document).ready(function(){
    $(document).on("contextmenu",function(e){
        if(e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA")
             e.preventDefault();
     });
 });

回答by Nijanthan

Give your id or class name to specify for which area you want to disable, for example:

提供您的 id 或类名以指定要禁用的区域,例如:

$("#your_id").bind("contextmenu", function(e) {
    e.preventDefault();
});

回答by Jitu

You can use this to disable for particular tag(replace img)

您可以使用它来禁用特定标签(替换 img)

$(document).ready(function() {
$("img").bind("contextmenu",function(){
   return false;
});});

Courtesy to Peeter How to prevent Right Click option using jquery

礼貌 Peeter如何使用 jquery 防止右键单击选项

回答by Milind Anantwar

For binding it with all element you need:

要将其与您需要的所有元素绑定:

$(document).ready(function(){
  $(document).bind("contextmenu",function(e){
     e.preventDefault();//or return false;
 });
 });

回答by Tushar Dave

Try this code

试试这个代码

   <input type="text" id="test1"/>
<input type="text" id="test2"/>

JS Code:

JS代码:

$( "#test1" ).on( "copy cut paste drop", function() {
            return false;
    });

Jsfiddle

提琴手