在这里我试图禁用 Ctrl+P 但它没有让我提醒并且它还显示了打印选项
jQuery(document).bind("keyup keydown", function(e){
if(e.ctrlKey && e.keyCode == 80){
alert('fine');
return false;
}
});
我不确定如何使用 jQuery 或 JavaScript 禁用 Ctrl+P 组合本身。
谢谢
最佳答案
你不能阻止用户打印,但你可以通过简单的 CSS 在用户打印文档时隐藏所有内容:
<style type="text/css" media="print">
* { display: none; }
</style>
如果您想在访问者尝试打印时向他/她显示一条自定义消息,而不仅仅是一个空白页,则可以使用客户端代码,但首先要像这样包装所有现有内容:
<div id="AllContent">
<!-- all content here -->
</div>
并添加带有自定义消息的容器:
<div class="PrintMessage">You are not authorized to print this document</div>
现在去掉 <style type="text/css" media="print">
block 和代码将是:
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();
}
代码取自this excellent answer .
Updated fiddle . (打印时显示消息)
关于javascript - 如何使用 javascript 或 Jquery 禁用 CTRL+P?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18843936/