JS禁用网页复制

内容来自:

https://zhuanlan.zhihu.com/p/348299601?utm_id=0

一、通过 JavaScript 实现

// 禁止右键菜单
document.oncontextmenu = function(){ return false; };
// 禁止文字选择
document.onselectstart = function(){ return false; };
// 禁止复制
document.oncopy = function(){ return false; };
// 禁止剪切
document.oncut = function(){ return false; };

允许复制需要把以上的false改为true

二、通过 HTML 实现

<body 
    oncopy="return false" 
    oncut="return false;" 
    onselectstart="return false" 
    oncontextmenu="return false"
>
<!--……-->
</body>

通过以下 JS 代码可以恢复页面复制、剪切及内容选中功能:


document.body.oncopy = null;
document.body.oncut = null;
document.body.onselectstart = null;
document.body.oncontextmenu = null;

三、通过 CSS 实现

body {
  -moz-user-select:none; /* Firefox私有属性 */
  -webkit-user-select:none; /* WebKit内核私有属性 */
  -ms-user-select:none; /* IE私有属性(IE10及以后) */
  -khtml-user-select:none; /* KHTML内核私有属性 */
  -o-user-select:none; /* Opera私有属性 */
  user-select:none; /* CSS3属性 */
}

恢复页面内容选中功能,需要通过 JS 代码进行实现:

document.body.style.webkitUserSelect = 'auto'; // Firefox
document.body.style.userSelect = 'auto'; // Chrome
文章链接:http://iwebg.cn/index.php/2023/07/26/js%e7%a6%81%e7%94%a8%e7%bd%91%e9%a1%b5%e5%a4%8d%e5%88%b6/

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。

评论

  1. zlhranhou
    3 年前
    2023-7-27 15:59:42

    有用!

    • 博主
      zlhranhou
      3 年前
      2023-7-27 16:00:42

      雀食

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇