Home > Web Front-end > JS Tutorial > How to prevent copying of text in Javascript

How to prevent copying of text in Javascript

autoload
Release: 2021-04-09 16:13:59
Original
2715 people have browsed it

How to prevent copying of text in Javascript

HTML content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>禁止选中文字和禁止右键菜单</title>
</head>
<body>
</body>
</html>
Copy after login

JS code:

<script>
         const p=document.querySelector(&#39;p&#39;);
         console.log(p);
         p.addEventListener("contextmenu",function(e){
            e.preventDefault();
         });
 </script>
Copy after login

Although the above code cannot be selected by right-clicking and the menu will appear, it can still be selected by using the shortcut key ctrl c Copy, so you can modify it as follows:

<script>
         const p=document.querySelector(&#39;p&#39;);
         console.log(p);
         p.addEventListener("contextmenu",function(e){
            e.preventDefault();
         });
          p.addEventListener("selectstart",function(e){
             e.preventDefault();
         });
 </script>
Copy after login

Recommended: "2021 js interview questions and answers (large summary)"

The above is the detailed content of How to prevent copying of text in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template