如何使用jQuery禁用复制、粘贴、剪切和右键

如何使用jQuery禁用复制、粘贴、剪切和右键

通过 “ctrl+c”、”ctrl+v”、”ctrl+x “和鼠标右键,可以从任何地方复制、粘贴和剪切任何东西。它可以被禁用于特定的任务或页面。让我们看看如何禁用剪切、复制、粘贴和右键点击。

它可以通过两种方式进行。

  • 通过使用on()方法
  • 通过使用keydown()和mousedown()方法

通过使用on()方法:它是jQuery中的一个内置方法。在这个方法的帮助下,我们将能够禁用剪切、复制、粘贴和右键点击选项。

  • 语法:
$(“selector”).on(event, function)
  • 示例:
<!DOCTYPE html>
<html>
  
<head>
    <title>The jQuery Example</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
    </script>
    <style>
        #geek {
            padding: 65 px 0;
        }
    </style>
  
    <script>
        (document).ready(function() {
            
            // Disables ctrl+v, ctrl+x, ctrl+c.
            ('textarea').on("cut", function(e) {
                ("#d2").text('cut. not allowed!');
                e.preventDefault();
            });
            ('textarea').on("copy", function(e) {
                ("#d2").text('copy. not allowed!');
                e.preventDefault();
            });
            ('textarea').on("paste", function(e) {
                ("#d2").text('paste. not allowed!');
                e.preventDefault();
            });
            
            // Above all three can be combined into one, above is 
            // executed separately for understanding purposes.
            /*('textarea').on("cut copy paste", function(e) {
            ("#d2").text('right-click is disabled!');
            e.preventDefault();
            }); */
            
            // Disables right-click.
            ('textarea').mousedown(function(e) {
                if (e.button == 2) {
                    e.preventDefault();
                    alert('right-click is disabled!');
                }
            });
        });
    </script>
</head>
  
<body>
    <center>
        <div id='geek'>
            <h1 style="color:green">GeeksforGeeks</h1>
            <p id="d1">
                The below textarea won't allow any cut, copy, 
                paste and right-click operations.
            </p>
            <textarea></textarea>
            <p id="d2" style="color:red"></p>
        </div>
    </center>
</body>
  
</html>
  • 输出:
    如何使用jQuery禁用复制、粘贴、剪切和右键?

通过使用keydown()和mousedown()方法:通过使用key-down事件,对于禁用右键,我们使用mouse-down()方法。在这两个方法的帮助下,我们将能够禁用剪切、复制、粘贴和右键点击选项。

  • 语法:
$(“selector”).keydown(function)
$(“selector”).mousedown(function)
  • 示例:
<!DOCTYPE html>
<html>
  
<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
    </script>
    <style>
        #geek {
            padding: 65px 0;
        }
    </style>
  
    <script>
        (document).ready(function() {
            (document).keydown(function(event) {
  
                // 86 is the keycode of v
                if (event.ctrlKey == true && (event.which == '86')) {
                    ("#d2").text('paste. not allowed!');
                    event.preventDefault();
                }
  
                // 88 is the keycode of x
                if (event.ctrlKey == true && (event.which == '88')) {
                    ("#d2").text('cut. not allowed!');
                    event.preventDefault();
                }
  
                // 67 is the keycode of c
                if (event.ctrlKey == true && (event.which == '67')) {
                    ("#d2").text('copy. not allowed!');
                    event.preventDefault();
                }
  
                // Above all three can be combined into one, above is 
                // executed separately for understanding purposes.
                /* if (event.ctrlKey==true && (event.which == '86' 
                || event.which == '67' || event.which == '88')) {
                    alert('cut. copy. paste. not allowed!');
                    event.preventDefault();
                } */
            });
            ('textarea').mousedown(function(e) {
                if (e.button == 2) {
                    alert('right-click is disabled!');
                    e.preventDefault();
                }
            });
        });
    </script>
</head>
  
<body>
    <center>
        <div id='geek'>
            <h1 style="color:green">GeeksforGeeks</h1>
            <p id="d1">
                The below textarea won't allow any cut, copy,
                paste and right-click operations.
            </p>
            <textarea></textarea>
            <p id="d2" style="color:red"></p>
        </div>
    </center>
</body>
  
</html>
  • 输出:
    如何使用jQuery禁用复制、粘贴、剪切和右键?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程