通过JS禁止相关的右键、快捷键、复制等操作

JavaScript
已认证 BOSS 2022-11-8

1069 0

禁用鼠标

禁止鼠标右键、禁止全选、复制、粘贴;

禁用右键菜单

oncontextmenu事件js代码:

document.oncontextmenu = function(){
    event.returnValue = false;
}
// 或者直接返回整个事件
document.oncontextmenu = function(){
    return false;
}

禁用网页上选取的内容

onselectstart事件js代码:

document.onselectstart = function(){
    event.returnValue = false;
}
// 或者直接返回整个事件
document.onselectstart = function(){
    return false;
}

禁用复制

oncopy事件js代码:

document.oncopy = function(){
    event.returnValue = false;
}
// 或者直接返回整个事件
document.oncopy = function(){
    return false;
}

直接操作body标签

以上都是通过js进行禁用,如果只想单纯的禁用鼠标右键、和复制粘贴,还可以将它们直接写到HTML中的body上面

<body oncontextmenu = "return false" ></body>
<body onselectstart = "return false" ></body>
<body oncopy = "return false" ></body>

只保留鼠标左键

document.onmousedown = function(e){
    if ( e.which == 2 ){// 鼠标滚轮的按下,滚动不触发
        return false;
    }
    if( e.which==3 ){// 鼠标右键
        return false;
    }
}

禁用键盘

禁用键盘中的ctrl、alt、shift

document.onkeydown = function(){
    if( event.ctrlKey ){
        return false;
    }
    if ( event.altKey ){
        return false;
    }
    if ( event.shiftKey ){
        return false;
    }
}
近段时间做甩手掌柜,不问事,有事请找管理。
最新回复 (0)
    • YiOVE论坛
      2
         
返回