javascript 使用 jquery 防止复制粘贴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15540162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
preventing copy paste using jquery
提问by Ramya
Need to prevent copy paste in a textbox using jquery. How to implement it?
需要防止使用 jquery 在文本框中复制粘贴。如何实施?
<table>
<tr>
<h:inputlabel value="Actual"></h:inputlabel>
<td>
<h:inputtext id="Actual" styleClass="input-tex" value="#bean.customer"></h:inputtext>
<td>
</tr>
<table>
回答by Vishal Suthar
Here to go: Disable Cut, Copy and Paste function for textbox using jQuery
$(document).ready(function(){
$('#Actual').bind("cut copy paste",function(e) {
e.preventDefault();
});
});
Note: Opera didn't support cut, copy and paste events before version 12.10
注意:Opera 在 12.10 版本之前不支持剪切、复制和粘贴事件
回答by equiman
It's the most and 'official' way to do it with Jquery.
这是使用 Jquery 执行此操作的最“官方”方式。
$(document).ready(function () {
var ambit = $(document);
// Disable Cut + Copy + Paste (input)
ambit.on('copy paste cut', function (e) {
e.preventDefault(); //disable cut,copy,paste
return false;
});
});
However, it only works on and as I read isn't supported on some Opera versions. Everything outside of an input is allowed to be copied.
但是,它仅适用于我所阅读的某些 Opera 版本不支持。允许复制输入之外的所有内容。
If what you want disable completely, paranoic mode: on
, you can use this method:
如果你想完全禁用paranoic mode: on
,你可以使用这个方法:
$(document).ready(function () {
var ambit = $(document);
// Disable Cut + Copy + Paste (input)
ambit.on('copy paste cut', function (e) {
e.preventDefault(); //disable cut,copy,paste
return false;
});
// Disable Cut + Copy + Paste and Browser Admin Tools (all document)
ambit.keydown(function (e) {
var forbiddenCtrlKeys = new Array('c', 'x', 'v', 'ins', 'u');
var forbiddenShiftKeys = new Array('del', 'ins', 'f2', 'f4', 'f7');
var forbiddenCtrlShiftKeys = new Array('k', 'i', 'm', 's', 'j');
var keyCode = (e.keyCode) ? e.keyCode : e.which;
var isCtrl, isShift;
isCtrl = e.ctrlKey;
isShift = e.ctrlShift;
string = getKeyCodeString(keyCode);
if (string == 'f12')
{
e.preventDefault();
return false;
}
if (isCtrl && !isShift) {
for (i = 0; i < forbiddenCtrlKeys.length; i++) {
if (forbiddenCtrlKeys[i] == string) {
e.preventDefault();
return false;
}
}
}
if (!isCtrl && isShift) {
for (i = 0; i < forbiddenShiftKeys.length; i++) {
if (forbiddenShiftKeys[i] == string) {
e.preventDefault();
return false;
}
}
}
if (isCtrl && isShift) {
for (i = 0; i < forbiddenCtrlShiftKeys.length; i++) {
if (forbiddenCtrlShiftKeys[i] == string) {
e.preventDefault();
return false;
}
}
}
return true;
});
var getKeyCodeString = function(keyCode)
{
var string;
switch (keyCode) {
case 45:
string = 'ins'; break;
case 46:
string = 'del'; break;
case 113:
string = 'f2'; break;
case 115:
string = 'f4'; break;
case 118:
string = 'f7'; break;
case 123:
string = 'f12'; break;
default:
string = String.fromCharCode(keyCode);
break;
}
return string.toLowerCase();
}
});
And what about the contextual menu?
那么上下文菜单呢?
$(document).ready(function () {
var ambit = $(document);
// Disable Contextual Menu
ambit.on('contextmenu', function (e) {
e.preventDefault();
return false;
});
And what about mobile?
手机呢?
$(document).ready(function () {
var ambit = $(document);
// Disable Tap and Hold (jQuery Mobile)
ambit.on('taphold', function (e) {
e.preventDefault();
return false;
});
});
Hope it helps! Corrections and improvements are Welcome!
希望能帮助到你!欢迎更正和改进!
回答by KillerCoder
As Jquery 1.9 onward live event is not supported, we can use "on" for same purpose.
由于不支持 Jquery 1.9 以后的实时事件,我们可以使用“on”来实现相同的目的。
$('#Actual').on("cut copy paste", function (e) {
e.preventDefault();
});