Javascript 如何使用 jQuery 禁用粘贴 (Ctrl+V)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5510129/
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-23 17:32:14  来源:igfitidea点击:

How to disable Paste (Ctrl+V) with jQuery?

javascriptjquerycopy-pastetextinput

提问by Misam

How can I disable Paste (Ctrl+V) option using jQuery in one of my input text fields?

如何在我的输入文本字段之一中使用 jQuery禁用粘贴 ( Ctrl+ V) 选项?

回答by Misam

This now works for IE FF Chrome properly... I have not tested for other browsers though

这现在适用于 IE FF Chrome ......不过我还没有测试其他浏览器

$(document).ready(function(){
   $('#txtInput').on("cut copy paste",function(e) {
      e.preventDefault();
   });
});

Edit: As pointed out by webeno, .bind()is deprecated hence it is recommended to use .on()instead.

编辑:正如webeno所指出的,.bind()已弃用,因此建议.on()改用。

回答by DannyLane

Edit: It's almost 6 years later, looking at this now I wouldn't recommend this solution. The accepted answer is definitely much better. Go with that!

编辑:差不多 6 年后,现在看这个我不推荐这个解决方案。接受的答案肯定要好得多。去吧!



This seems to work.

这似乎有效。

You can listen to keyboard eventswith jQuery and prevent the event from completing if its the key combo you are looking for. Note, check 118 and 86 (Vand v)

您可以使用 jQuery侦听键盘事件,如果它是您正在寻找的组合键,则可以防止该事件完成。注意,检查 118 和 86(Vv

Working example here: http://jsfiddle.net/dannylane/9pRsx/4/

这里的工作示例:http: //jsfiddle.net/dannylane/9pRsx/4/

$(document).ready(function(){
    $(document).keydown(function(event) {
        if (event.ctrlKey==true && (event.which == '118' || event.which == '86')) {
            alert('thou. shalt. not. PASTE!');
            event.preventDefault();
         }
    });
});

Update: keypress doesn't fire in IE, use keydown instead.

更新:keypress 不会在 IE 中触发,请改用 keydown。

回答by Steve Chan

As of JQuery 1.7 you might want to use the on method instead

从 JQuery 1.7 开始,您可能希望改用 on 方法

$(function(){
    $(document).on("cut copy paste","#txtInput",function(e) {
        e.preventDefault();
    });
});

回答by Abhishek

I tried this in my Angular project and it worked fine without jQuery.

我在我的 Angular 项目中尝试过这个,它在没有 jQuery 的情况下运行良好。

<input type='text' ng-paste='preventPaste($event)'>

And in script part:

在脚本部分:

$scope.preventPaste = function(e){
   e.preventDefault();
   return false;
};

In non angular project, use 'onPaste' instead of 'ng-paste' and 'event' instesd of '$event'.

在非角度项目中,使用 'onPaste' 而不是 'ng-paste' 和 'event' instesd of '$event'。

回答by hawx

jQuery('input.disablePaste').keydown(function(event) {
    var forbiddenKeys = new Array('c', 'x', 'v');
    var keyCode = (event.keyCode) ? event.keyCode : event.which;
    var isCtrl;
    isCtrl = event.ctrlKey
    if (isCtrl) {
        for (i = 0; i < forbiddenKeys.length; i++) {
            if (forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
                 return false;
            }
        }
    }
    return true;
});

回答by JoyGuru

The following code will disable cut, copy and paste from full page.

以下代码将禁用从整页剪切、复制和粘贴。

$(document).ready(function () {
   $('body').bind('cut copy paste', function (e) {
      e.preventDefault();
   });
});

The full tutorial and working demo can be found from here - Disable cut, copy and paste using jQuery

可以从这里找到完整的教程和工作演示 -使用 jQuery 禁用剪切、复制和粘贴

回答by Gtm

 $(document).ready(function(){
   $('#txtInput').on("cut copy paste",function(e) {
      e.preventDefault();
   });
});
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="text" id="txtInput" />

回答by Chirag Prajapati

I have tested the issue on chrome browser and it is working for me.Below is a solution for preventing the paste code in your textbox and also prevent the right click.

我已经在 chrome 浏览器上测试了这个问题,它对我有用。下面是一个防止在文本框中粘贴代码并防止右键单击的解决方案。

   $(".element-container").find('input[type="text"]').live("contextmenu paste", function (e) {

    e.preventDefault();
});

回答by Vikas Bansal

$(document).ready(function(){
  $('#txtInput').live("cut copy paste",function(e) {
    e.preventDefault();
  });
});

On textbox live event cut, copy, paste event is prevented and it works well.

在文本框实时事件剪切,复制,粘贴事件被阻止并且它运行良好。

回答by canardman

You can catch key event :

您可以捕获关键事件:

function checkEventObj ( _event_ ){
    // --- IE explorer
    if ( window.event )
        return window.event;
    // --- Netscape and other explorers
    else
        return _event_;
}

document.keydown = function(_event) {
    var e = checkEventObject(_event);

    if( e.ctrlKey && (e.keyCode == 86) )
        window.clipboardData.clearData();
}

Not tested but, could help.

未测试,但可以提供帮助。

Source from comentcamarcheand Zakaria

来源来自commentcamarche和Zakaria