Ram*_*nan 158 html javascript jquery
如何使用Javascript 检测ctrl+ v,ctrl+ c?
我需要限制粘贴在我的textareas中,最终用户不应该复制和粘贴内容,用户应该只在textarea中键入文本.
怎么做到这一点?
jac*_*cnr 172
我出于兴趣而这样做了.我同意这不是正确的事情,但我认为这应该是操作的决定......而且代码可以很容易地扩展到添加功能,而不是把它带走(比如更高级的剪贴板,或Ctrl+ s触发服务器 - 边保存).
$(document).ready(function() {
var ctrlDown = false,
ctrlKey = 17,
cmdKey = 91,
vKey = 86,
cKey = 67;
$(document).keydown(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = true;
}).keyup(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = false;
});
$(".no-copy-paste").keydown(function(e) {
if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey)) return false;
});
// Document Ctrl + C/V
$(document).keydown(function(e) {
if (ctrlDown && (e.keyCode == cKey)) console.log("Document catch Ctrl+C");
if (ctrlDown && (e.keyCode == vKey)) console.log("Document catch Ctrl+V");
});
});
Run Code Online (Sandbox Code Playgroud)
另外,澄清一下,这个脚本需要jQuery库.
编辑:删除3条冗余线(涉及e.which)感谢Tim Down的建议(见评论)
编辑:添加了对Mac的支持(cmd键而不是ctrl)
Chr*_*son 52
使用jquery,您可以通过绑定函数轻松检测复制,粘贴等:
$("#textA").bind('copy', function() {
$('span').text('copy behaviour detected!')
});
$("#textA").bind('paste', function() {
$('span').text('paste behaviour detected!')
});
$("#textA").bind('cut', function() {
$('span').text('cut behaviour detected!')
});
Run Code Online (Sandbox Code Playgroud)
更多信息请访问:http://www.mkyong.com/jquery/how-to-detect-copy-paste-and-cut-behavior-with-jquery/
cry*_*ryo 38
虽然在用作反盗版措施时可能会很烦人,但我可以看到可能存在一些合法的情况,因此:
function disableCopyPaste(elm) {
// Disable cut/copy/paste key events
elm.onkeydown = interceptKeys
// Disable right click events
elm.oncontextmenu = function() {
return false
}
}
function interceptKeys(evt) {
evt = evt||window.event // IE support
var c = evt.keyCode
var ctrlDown = evt.ctrlKey||evt.metaKey // Mac support
// Check for Alt+Gr (http://en.wikipedia.org/wiki/AltGr_key)
if (ctrlDown && evt.altKey) return true
// Check for ctrl+c, v and x
else if (ctrlDown && c==67) return false // c
else if (ctrlDown && c==86) return false // v
else if (ctrlDown && c==88) return false // x
// Otherwise allow
return true
}
Run Code Online (Sandbox Code Playgroud)
我已经使用event.ctrlKey
而不是检查密钥代码,因为在Mac OS X上的大多数浏览器Ctrl/ Alt"向下"和"向上"事件从未被触发,因此检测的唯一方法是event.ctrlKey
在Ctrl密钥后使用例如c事件压制住了.我也ctrlKey
用metaKey
mac 代替了.
此方法的局限性:
edit
- > copy
菜单项仍然可以允许复制/粘贴.cry*_*ryo 13
有这样做的另一种方法: onpaste
,oncopy
和oncut
事件可以注册并在IE,火狐,Chrome,Safari浏览器(有一些小问题)取消,唯一的主要的浏览器不允许取消这些活动是Opera.
你可以在我的另一个答案中看到截取Ctrl+ v和Ctrl+ c有很多副作用,它仍然不会阻止用户使用Firefox Edit
菜单粘贴等.
function disable_cutcopypaste(e) {
var fn = function(evt) {
// IE-specific lines
evt = evt||window.event
evt.returnValue = false
// Other browser support
if (evt.preventDefault)
evt.preventDefault()
return false
}
e.onbeforepaste = e.onbeforecopy = e.onbeforecut = fn
e.onpaste = e.oncopy = e.oncut = fn
}
Run Code Online (Sandbox Code Playgroud)
Safari仍然存在一些使用此方法的小问题(当防止默认时,它会清除剪贴板而不是剪切/复制)但是现在Chrome中已经修复了该错误.
另请参阅: http ://www.quirksmode.org/dom/events/cutcopypaste.html以及相关的测试页面http://www.quirksmode.org/dom/events/tests/cutcopypaste.html以获取更多信息.
现场演示:http: //jsfiddle.net/abdennour/ba54W/
$(document).ready(function() {
$("#textA").bind({
copy : function(){
$('span').text('copy behaviour detected!');
},
paste : function(){
$('span').text('paste behaviour detected!');
},
cut : function(){
$('span').text('cut behaviour detected!');
}
});
});
Run Code Online (Sandbox Code Playgroud)
小智 8
防止用户使用上下文菜单,复制和剪切jQuery的简短解决方案:
jQuery(document).bind("cut copy contextmenu",function(e){
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
同样禁用CSS中的文本选择可能会派上用场:
.noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Run Code Online (Sandbox Code Playgroud)
如果您使用该ctrlKey
属性,则不需要维护状态。
$(document).keydown(function(event) {
// Ctrl+C or Cmd+C pressed?
if ((event.ctrlKey || event.metaKey) && event.keyCode == 67) {
// Do stuff.
}
// Ctrl+V or Cmd+V pressed?
if ((event.ctrlKey || event.metaKey) && event.keyCode == 86) {
// Do stuff.
}
// Ctrl+X or Cmd+X pressed?
if ((event.ctrlKey || event.metaKey) && event.keyCode == 88) {
// Do stuff.
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
另一种方法(不需要插件)它只使用传入的事件对象ctrlKey
的属性。它指示事件发生时是否被按下,如下所示:Ctrl
$(document).keypress("c",function(e) {
if(e.ctrlKey)
alert("Ctrl+C was pressed!!");
});
Run Code Online (Sandbox Code Playgroud)
另请参见jquery:按键、ctrl+c (或类似的组合)。
归档时间: |
|
查看次数: |
236802 次 |
最近记录: |