原文: https://howtodoinjava.com/jquery/jquery-detect-cut-copy-or-paste-events/

    如果您从事的项目涉及具有客户端输入验证的动态 UI,那么您可以回想起 QA / 测试人员非常普遍的做法,即他们尝试复制大量垃圾文本以创建无效的方案并将它们记录为错误。 我不会评论这类问题的有效性。 在本文的这篇文章中,我将为您提供一个解决方案,如果遇到此问题,您可以使用该解决方案。

    请注意,这将始终有效。 无论您使用键盘还是鼠标生成这些事件。

    jQuery – 检测剪切,复制或粘贴事件 - 图1

    阅读更多按键事件和击键事件之间的差异检测ENTER

    检测剪切复制或粘贴事件

    要检测这些事件,您需要以给定的方式为任何输入区域绑定以下事件。

    1. $(document).ready(function() {
    2. $("#textA").bind({
    3. copy : function(){
    4. $('#message').text('copy behaviour detected!');
    5. },
    6. paste : function(){
    7. $('#message').text('paste behaviour detected!');
    8. },
    9. cut : function(){
    10. $('#message').text('cut behaviour detected!');
    11. }
    12. });
    13. });

    尝试一下

    1. <html>
    2. <head>
    3. <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    4. <style type="text/css">
    5. span{
    6. color:green;
    7. font-weight:bold;
    8. }
    9. </style>
    10. </head>
    11. <body>
    12. <h1>Detect Cut, Copy and Paste with jQuery</h1>
    13. <form action="#">
    14. <label>Text Box : </label>
    15. <input id="textA" type="text" size="50" value="Copy, paste or cut any text here" />
    16. </form>
    17. <span id="message"></span>
    18. <script type="text/javascript">
    19. $(document).ready(function() {
    20. $("#textA").bind({
    21. copy : function(){
    22. $('#message').text('copy behaviour detected!');
    23. },
    24. paste : function(){
    25. $('#message').text('paste behaviour detected!');
    26. },
    27. cut : function(){
    28. $('#message').text('cut behaviour detected!');
    29. }
    30. });
    31. });
    32. </script>
    33. </body>
    34. </html>

    祝您学习愉快!