阻止用户使用jQuery复制文本?

时间:2011-04-17 07:52:02

标签: jquery asp.net html css html5

如何阻止用户使用jQuery复制asp.net页面中的任何文本?

4 个答案:

答案 0 :(得分:9)

您可以禁用右键单击并在文档上绑定keyup事件以检测复制命令组合键“Ctl + C”并返回false。

要禁用右键单击:

jQuery(document).bind("contextmenu", function(e) {
 e.preventDefault();
});

检测Ctl + C:

jQuery(document).ready(function()
{
    var ctlPressed = false; //Flag to check if pressed the CTL key
    var ctl = 17; //Key code for Ctl Key
    var c = 67; //Key code for "c" key

    jQuery(document).keydown(function(e)
    {
        if (e.keyCode == ctl) 
          ctlPressed = true;
    }).keyup(function(e)
    {
        if (e.keyCode == ctl) 
          ctlPressed = false;
    });

    jQuery(".your-no-copy-area").keydown(function(e)
    {
        if (ctlPressed && e.keyCode == c) 
          return false;
    });
});

答案 1 :(得分:1)

水印是您的解决方案。我可以轻松禁用Javascript。

答案 2 :(得分:1)

这通常不赞成,但如果你必须这样做,here is a plugin

答案 3 :(得分:1)

我使用了很多代码来制作它:

1 - 禁用右键单击:

<script src="js/jquery.min.js" type="text/javascript"></script>    
<script type="text/javascript" language="javascript">
    $(function () {
        $(this).bind("contextmenu", function (e) {
            e.preventDefault();
            alert("Copy is not allowed");
        });
    });       
</script>

2 - 禁用选择

<script type="text/javascript">

    /***********************************************
    * Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
    ***********************************************/

    function disableSelection(target) {
        if (typeof target.onselectstart != "undefined") //IE route
            target.onselectstart = function () { return false }
        else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
            target.style.MozUserSelect = "none"
        else //All other route (ie: Opera)
            target.onmousedown = function () { return false }
        target.style.cursor = "default"
    }

    //Sample usages
    //disableSelection(document.body) //Disable text selection on entire body
    //disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"

         var alltables = document.getElementsByTagName(“table”)     for(var i = 0; i&lt; alltables.length; i ++)         disableSelection(alltables [i])//禁用页面上所有表中的文本选择

3-I我将这些添加到正文标记的末尾

<script type="text/javascript">
     var somediv = document.getElementById("page-wrap")
     disableSelection(somediv) //disable text selection within DIV with id="page-wrap"
    </script>
<script type="text/javascript">
    disableSelection(document.body) //disable text selection on entire body of page
</script>

现在全部完成.....

谢谢你们,这真的非常有帮助。