我想阻止用户:
>从第一个文本框复制并粘贴到第二个文本框
>右键单击并使用上下文菜单从第一个文本框复制并粘贴到第二个文本框.
这不起作用.
<html> <head runat="server"> <title>Confirm email page</title> <script type="text/javascript" language="javascript"> function DisableRightClick(event) { //For mouse right click if (event.button == 2) { } } function DisableCtrlKey(e) { var code = (document.all) ? event.keyCode : e.which; // look for CTRL key press if (parseInt(code) == 17) { window.event.returnValue = false; } } </script> </head> <body style="font-family: Verdana; font-size: 1em"> <form id="form1" runat="server"> <div> <h1>Confirm Email</h1> <asp:Label ID="Label2" runat="server" Text="Enter Email Address: "></asp:Label> <asp:TextBox ID="TextBox2" runat="server" oncopy="return false" onMouseDown="DisableRightClick(event)" ></asp:TextBox><br /> <asp:Label ID="Label3" runat="server" Text="Confirm Email Address: "></asp:Label> <asp:TextBox ID="TextBox3" runat="server" onKeyDown="return DisableCtrlKey(event)"></asp:TextBox><br /> </div> </form> </body> </html>
GOT IT WORKING !!!!
<div> <h1>Copy Paste Preventer!!!</h1> <asp:Label ID="Label1" runat="server" Text="Enter Username: "></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:Label ID="Label2" runat="server" Text="Enter Email Address: "></asp:Label> <asp:TextBox ID="email" runat="server" oncopy="return false" onpaste="return false" oncut="return false" oncontextmenu="forms[0].elements[0].value++; return false"></asp:TextBox><br /> <asp:Label ID="Label3" runat="server" Text="Confirm Email Address: "></asp:Label> <asp:TextBox ID="TextBox3" runat="server" oncopy="return false" onpaste="return false" oncut="return false" oncontextmenu="forms[0].elements[0].value++; return false"> </asp:TextBox><br /> </div>
解决方法
使用jQuery时,这非常简单,并且与ASP.NET完全兼容:
<script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'> </script> <script type="text/javascript"> $(function() { $('input[id$=TextBox2]').bind('cut copy paste',function(e) { e.preventDefault(); alert('You cannot ' + e.type + ' text!'); }); }); </script>
这篇文章解释了它如何与ASP.NET一起工作:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=398
正如斯科特指出的那样:在生产中,你应该把jQuery引用放在html的底部(但仍然在body标签内).
UPDATE
由于您要求完全阻止上下文菜单,您可以执行以下操作:
添加此脚本:
<script type="text/javascript"> document.getElementById('TextBox2').oncontextmenu = function (){ return false; }; </script>
返回false时,不显示菜单项.以下是browserupport的概述:
http://help.dottoro.com/ljhwjsss.php