Heim  >  Artikel  >  Web-Frontend  >  So verhindern Sie das Kopieren von Website-Inhalten

So verhindern Sie das Kopieren von Website-Inhalten

藏色散人
藏色散人nach vorne
2020-03-04 13:51:143105Durchsuche

Wenn Sie nicht möchten, dass andere den Inhalt Ihrer Website kopieren, können Sie diesen JS-Code zu Ihrer Webseite hinzufügen, um das Rechtsklickmenü, das Kopieren und Einfügen, die Auswahl usw. zu blockieren.

Empfohlen: „js-Tutorial

Manchmal besteht die Anforderung darin, dass es einige Inhalte auf der Website gibt, von denen Sie nicht möchten, dass andere sie sehen kopieren, daher müssen Sie die Codesteuerung verwenden.

Es gibt viele Methoden:

Die erste Methode:

//屏蔽右键菜单 
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; 

    } 

} 


//屏蔽选中 

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; 

    } 

}

Die zweite Methode:

Fügen Sie den folgenden js-Code hinzu

<script type="text/javascript">

// oncontextmenu 事件在元素中用户右击鼠标时触发并打开上下文菜单

document.oncontextmenu=new Function("event.returnValue=false"); 

// onselectstart几乎可以用于所有对象,其触发时间为目标对象被开始选中时(即选中动作刚开始,尚未实质性被选中)

document.onselectstart=new Function("event.returnValue=false"); 

</script>
例子:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<style>

*{margin: 0;padding: 0;}

.container h1 {color: gold;text-align:center;margin-bottom:30px;}

.container p {width: 500px;margin:0 auto;color: purple;text-indent: 30px;}

</style>

</head>

<body>

<div>

<h1>火影忍者</h1>

<p>

十多年前一只拥有巨大威力的妖兽“九尾妖狐”袭击了木叶忍者村,当时的第四代火影拼尽全力,以自己的生命为代价将“九尾妖狐”封印在了刚出生的鸣人身上。木叶村终于恢复了平静,但村民们却把鸣人当成像“九尾妖狐”那样的怪物看待,所有人都疏远他。 鸣人自小就孤苦无依,一晃十多年过去了,少年鸣人考入了木叶村的忍者学校,结识了好朋友佐助和小樱。佐助是宇智波家族的传人之一,当他还是小孩的时候他的哥哥——一个已经拥有高超忍术的忍者将他们家族的人都杀死了,然后投靠了一直想将木叶村毁灭的大蛇丸,佐助自小就发誓要超越哥哥,为家族报仇。鸣人他们在忍者学校得到了教官卡卡西的精心指点,在他的帮助下去迎接成长中的一次又一次挑战! 

</p>

</div>

<!-- 第二种方法:通过js代码实现 -->

<script type="text/javascript">

// oncontextmenu 事件在元素中用户右击鼠标时触发并打开上下文菜单

document.oncontextmenu=new Function("event.returnValue=false"); 

// onselectstart几乎可以用于所有对象,其触发时间为目标对象被开始选中时(即选中动作刚开始,尚未实质性被选中)

document.onselectstart=new Function("event.returnValue=false"); 

</script>

</body>

</html>

Die dritte Methode:

in 6c04bd5ca3fcae76e30b72ad730ca86d Folgender Code:

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

或 

<body oncontextmenu="event.returnValue=false" onselectstart="event.returnValue=false">

 

body中加入代码的这种方法有个缺陷就是取决于body的内容,如果body内容较少,从body下方往上选中内容,仍然是可以复制网站的内容的。

Vierte Methode:

Wenn Sie nur das Kopieren einschränken, können Sie den folgenden Code in 6c04bd5ca3fcae76e30b72ad730ca86d hinzufügen:

<body oncopy="alert(&#39;对不起,禁止复制!&#39;);return false;"> 
例子:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<style>

*{margin: 0;padding: 0;}

.container h1 {color: gold;text-align:center;margin-bottom:30px;}

.container p {width: 500px;margin:0 auto;color: purple;text-indent: 30px;}

</style>

</head>

<body oncopy="alert(&#39;对不起,禁止复制!&#39;);return false;">

<div>

<h1>火影忍者</h1>

<p>

十多年前一只拥有巨大威力的妖兽“九尾妖狐”袭击了木叶忍者村,当时的第四代火影拼尽全力,以自己的生命为代价将“九尾妖狐”封印在了刚出生的鸣人身上。木叶村终于恢复了平静,但村民们却把鸣人当成像“九尾妖狐”那样的怪物看待,所有人都疏远他。 鸣人自小就孤苦无依,一晃十多年过去了,少年鸣人考入了木叶村的忍者学校,结识了好朋友佐助和小樱。佐助是宇智波家族的传人之一,当他还是小孩的时候他的哥哥——一个已经拥有高超忍术的忍者将他们家族的人都杀死了,然后投靠了一直想将木叶村毁灭的大蛇丸,佐助自小就发誓要超越哥哥,为家族报仇。鸣人他们在忍者学校得到了教官卡卡西的精心指点,在他的帮助下去迎接成长中的一次又一次挑战! 

</p>

</div>

</body>

</html>

Die fünfte Methode:

Strg+C und Strg+V deaktivieren, Code:

// 禁用Ctrl+C和Ctrl+V(所有浏览器均支持)

$(document).keydown(function(e) {

  if(e.ctrlKey && (e.keyCode == 86 || e.keyCode == 67)) {

    return false;

  }

});
例子

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<style>

*{margin: 0;padding: 0;}

.container h1 {color: gold;text-align:center;margin-bottom:30px;}

.container p {width: 500px;margin:0 auto;color: purple;text-indent: 30px;}

</style>

</head>

<body>

<div>

<h1>火影忍者</h1>

<p>

十多年前一只拥有巨大威力的妖兽“九尾妖狐”袭击了木叶忍者村,当时的第四代火影拼尽全力,以自己的生命为代价将“九尾妖狐”封印在了刚出生的鸣人身上。木叶村终于恢复了平静,但村民们却把鸣人当成像“九尾妖狐”那样的怪物看待,所有人都疏远他。 鸣人自小就孤苦无依,一晃十多年过去了,少年鸣人考入了木叶村的忍者学校,结识了好朋友佐助和小樱。佐助是宇智波家族的传人之一,当他还是小孩的时候他的哥哥——一个已经拥有高超忍术的忍者将他们家族的人都杀死了,然后投靠了一直想将木叶村毁灭的大蛇丸,佐助自小就发誓要超越哥哥,为家族报仇。鸣人他们在忍者学校得到了教官卡卡西的精心指点,在他的帮助下去迎接成长中的一次又一次挑战! 

</p>

</div>

 

<script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>

<script>

$(document).keydown(function(e) {

 if(e.ctrlKey && (e.keyCode == 86 || e.keyCode == 67)) {

 alert(&#39;不能Ctrl+C和Ctrl+V复制、粘贴&#39;);

   return false;

 }

});

</script>

</body>

</html>

Wenn es Fehler gibt, kontaktieren Sie mich bitte, um diese zu korrigieren. Vielen Dank! ! !

Weitere Inhalte zum Thema Programmierung finden Sie in der Spalte Einführung in die Programmierung auf der chinesischen PHP-Website!

Das obige ist der detaillierte Inhalt vonSo verhindern Sie das Kopieren von Website-Inhalten. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:cnblogs.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen