如何使用javascript或jQuery禁用CTRL + P? [英] How to Disable the CTRL+P using javascript or Jquery?
问题描述
在这里,我尝试禁用 Ctrl + P ,但是它没有使我警觉,并且还显示了打印选项
Here I tried to disable the Ctrl+P but it doesn't get me alert and also it shows the print options
jQuery(document).bind("keyup keydown", function(e){
if(e.ctrlKey && e.keyCode == 80){
alert('fine');
return false;
}
});
我不确定如何使用jQuery或JavaScript禁用 Ctrl + P 组合本身.
I am not sure how can I disable the Ctrl+P combination itself using jQuery or JavaScript.
谢谢
推荐答案
您不能阻止用户进行打印,但是当用户使用简单的CSS打印文档时,您可以隐藏所有内容:
You can't prevent the user from printing, but you can hide everything when the user prints the document by using simple CSS:
<style type="text/css" media="print">
* { display: none; }
</style>
如果您想在访客尝试打印而不是空白页时向其显示自定义消息,则可以使用客户端代码,但首先将所有现有内容包装如下:
If you would like to show the visitor a custom message when he/she try to print rather then just a blank page, it's possible with client side code but first wrap all your existing contents like this:
<div id="AllContent">
<!-- all content here -->
</div>
并使用自定义消息添加这样的容器:
And add such a container with the custom message:
<div class="PrintMessage">You are not authorized to print this document</div>
现在摆脱<style type="text/css" media="print">
块,代码将是:
Now get rid of the <style type="text/css" media="print">
block and the code would be:
if ('matchMedia' in window) {
// Chrome, Firefox, and IE 10 support mediaMatch listeners
window.matchMedia('print').addListener(function(media) {
if (media.matches) {
beforePrint();
} else {
// Fires immediately, so wait for the first mouse movement
$(document).one('mouseover', afterPrint);
}
});
} else {
// IE and Firefox fire before/after events
$(window).on('beforeprint', beforePrint);
$(window).on('afterprint', afterPrint);
}
function beforePrint() {
$("#AllContent").hide();
$(".PrintMessage").show();
}
function afterPrint() {
$(".PrintMessage").hide();
$("#AllContent").show();
}
代码是从这个出色的答案中采用的.
更新了小提琴. (打印时显示消息)
Updated fiddle. (showing message when printing)
这篇关于如何使用javascript或jQuery禁用CTRL + P?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!