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>
JS code:
<script> const p=document.querySelector('p'); console.log(p); p.addEventListener("contextmenu",function(e){ e.preventDefault(); }); </script>
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('p'); console.log(p); p.addEventListener("contextmenu",function(e){ e.preventDefault(); }); p.addEventListener("selectstart",function(e){ e.preventDefault(); }); </script>
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!