个人收藏前端知识点

JS屏蔽按键(包括复制、粘贴、选中、剪切、F12等)

2018-12-20  本文已影响39人  AMONTOP

1、屏蔽F12或右键打开审查元素

window.onload = function () {
        
        //禁止F12
        $("*").keydown(function (e) {//判断按键
            e = window.event || e || e.which;
            if (e.keyCode == 123) {
                e.keyCode = 0;
                return false;
            }
        });
        
        //禁止审查元素
        $(document).bind("contextmenu",function(e){
            return false;
        });
    };

注意,这种方法并不能彻底禁止打开开发者工具!

2、屏蔽右键菜单

document.oncontextmenu = function (event){
if(window.event){
    event = window.event;
}try{
    var the = event.srcElement;
    if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
        return false;
    }
    return true;
}catch (e){
    return false;
}
}

三、屏蔽粘贴

document.onpaste = function (event){
if(window.event){
event = window.event;
}
try{
    var the = event.srcElement;
    if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
        return false;
    }
    return true;
}catch (e){
    return false;
}
}

四、屏蔽复制

document.oncopy = function (event){
if(window.event){
event = window.event;
}
try{
    var the = event.srcElement;
    if(!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
        return false;
    }
    return true;
}catch (e){
    return false;
}
}

五、屏蔽剪切

document.oncut = function (event){
if(window.event){
event = window.event;
}
try{
    var the = event.srcElement;
    if(!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
        return false;
    }
    return true;
}catch (e){
    return false;
}
}

这种很适合小说网站,毕竟版权珍贵,被别人随意copy走内容就不好了

六、屏蔽选中

document.onselectstart = function (event){
if(window.event){
    event = window.event;
}
try{
    var the = event.srcElement;
    if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
        return false;
     }
    return true;
} catch (e) {
    return false;
 }
}
上一篇下一篇

猜你喜欢

热点阅读