如何使用JavaScript禁用Ctrl + U [英] How to disable Ctrl+U using Javascript
本文介绍了如何使用JavaScript禁用Ctrl + U的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想禁用 Ctrl + U 和 Ctrl + C 事件.这样做的主要目的是防止用户轻松下载任何图像或从我的网站复制内容,即按 Ctrl + U 来查看我的网页的源代码或按 Ctrl + C 可直接从我的网页复制内容.
I just want to disable Ctrl+U and Ctrl+C event. The primary purpose of doing this is, preventing users from downloading any image or copying content from my website easily i.e. pressing Ctrl+U for viewing my webpage's source code or pressing Ctrl+C for copying content directly from my webpage.
当前,我正在使用这段代码,但是它禁用了我的整个键盘
Currently, I am using this piece of code but it disables my entire keyboard
<script>
/*function check(e)
{
alert(e.keyCode);
}*/
document.onkeydown = function(e) {
if (e.ctrlKey && (e.keyCode === 67 || e.keyCode === 86 || e.keyCode === 85 || e.keyCode === 117)) {//Alt+c, Alt+v will also be disabled sadly.
alert('not allowed');
}
return false;
};
</script>
推荐答案
这是我最后要禁用的 Ctrl + U :
This is finally what I got to disable Ctrl+U:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 67 ||
e.keyCode === 86 ||
e.keyCode === 85 ||
e.keyCode === 117)) {
return false;
} else {
return true;
}
};
$(document).keypress("u",function(e) {
if(e.ctrlKey)
{
return false;
}
else
{
return true;
}
});
</script>
这篇关于如何使用JavaScript禁用Ctrl + U的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文