如何使用jQuery禁用右键选项

如何使用jQuery禁用右键选项

jQuery中的bind()方法是用来为选定的元素附加一个或多个事件处理程序,这个方法指定了一个事件发生时要运行的函数。

语法:

$(selector).bind(event, data, function);

参数:该方法接受上面提到的和下面描述的三个参数。

  • event。它是一个事件类型,被传递给选定的元素。
  • data。它是可以在选定的元素上显示的数据。
  • function。它是由所选元素执行的功能。

返回值:它返回在所选元素上进行的所有修改。

在这篇文章中,我们将看到如何使用jQuery bind()方法来禁用右键点击选项。

<!DOCTYPE html>
<html>
  
<head>
  
    <!-- Adding jQuery scripts required to run jQuery -->
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    </script>
  
    <!-- This script will prevent 
        right click -->
    <script>
        (document).ready(function () {
            (document).bind("contextmenu", function (e) {
                return false;
            });
        });
    </script>
  
    <!-- Adding style -->
    <style>
        h1 {
            color: green;
        }
        p {
            color: crimson;
        }
    </style>
</head>
  
<!-- Body of the page -->
<body>
    <center>
        <h1>GeeksForGeeks</h1>
  
        <p>
            GeeksforGeeks is a Computer Science
            portal for geeks. It contains well
            written, well thought and well
            explained computer science and
            programming articles, quizzes etc.
        </p>
    </center>
</body>
  
</html>

输出:

如何使用jQuery禁用右键选项?

你可以看到,你不能通过在屏幕上点击右键来打开右键选项窗口。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程