html禁止复制文字的方法:1、禁用选中和右键;2、禁止选中文字;3、禁用F12按键;4、禁用调试工具。

本教程操作环境:windows7系统、html5版,DELL G3电脑。
html禁止复制文字的方法:
1、禁用选中和右键:
在<body>标签中添加以下代码:
立即学习“前端免费学习笔记(深入)”;
οncοntextmenu='return false' //禁止右键
οndragstart='return false' //禁止拖动
onselectstart ='return false' //禁止选中
οnselect='document.selection.empty()' //禁止选中
οncοpy='document.selection.empty()' //禁止复制
onbeforecopy='return false' // 禁止复制
οnmοuseup='document.selection.empty()'
<body leftmargin=0 topmargin=0 οncοntextmenu='return false' οndragstart='return false' onselectstart ='return false' οnselect='document.selection.empty()' οncοpy='document.selection.empty()' onbeforecopy='return false' οnmοuseup='document.selection.empty()'>
2、禁止选中文字
*{
    moz-user-select: -moz-none;
    -moz-user-select: none;
    -o-user-select:none;
    -khtml-user-select:none;
    -webkit-user-select:none;
    -ms-user-select:none;
    user-select:none;
}这时正常的选择复制都已经被禁用但是如果是程序员还知道可以用浏览器的查看源码和调试工具来直接从代码中复制内容。所以:
3、禁用F12按键
//禁用F12
window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
    // 判断是否按下F12,F12键码为123
    if (event.keyCode == 123) {
    event.preventDefault(); // 阻止默认事件行为
    window.event.returnValue = false;
    }
}
4、禁用调试工具
var threshold = 160; // 打开控制台的宽或高阈值
// 每秒检查一次
var check = setInterval(function() {
    if (window.outerWidth - window.innerWidth > threshold || 
        window.outerHeight - window.innerHeight > threshold) {
        // 如果打开控制台,则刷新页面
        window.location.reload();
    }
}, 1000)相关学习推荐:html视频教程
以上就是html如何禁止复制文字的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
 
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号