Aly*_*ono 13 html javascript jquery google-chrome printing-web-page
我一直在使用下面的代码打印我的页面:
window.print();
Run Code Online (Sandbox Code Playgroud)
下面的图片是Google Chrome浏览器中的打印预览.它有两个主要按钮:print和cancel.

我想知道用户是否单击了print或cancel按钮.我做的是使用jquery:
打印预览的HTML代码:
<button class="print default" i18n-content="printButton">Print</button>
<button class="cancel" i18n-content="cancel">Cancel</button>
Run Code Online (Sandbox Code Playgroud)
Jquery代码:
$('button > .cancel').click(function (e) {
alert('Cancel');
});
$('button > .print').click(function (e) {
alert('Print');
});
Run Code Online (Sandbox Code Playgroud)
我试过上面的代码没有运气.我在哪里错过了?
Pra*_*ara 15
您无法直接从常规网页访问Chrome的内部窗口(本例中为打印对话框).
(function () {
var beforePrint = function () {
alert('Functionality to run before printing.');
};
var afterPrint = function () {
alert('Functionality to run after printing');
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function (mql) {
//alert($(mediaQueryList).html());
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
}());
Run Code Online (Sandbox Code Playgroud)
或者如果您想在打印预览打开时执行某些操作,可以尝试以下操作.
$(document).bind("keyup keydown", function (e) {
if (e.ctrlKey && e.keyCode == 80) {
setTimeout(function () { CallAfterWindowLoad();}, 5000);
return true;
}
});
function CallAfterWindowLoad()
{
alert("Open and call");
}
Run Code Online (Sandbox Code Playgroud)
参考: 如何在Javascript window.print()调用的默认打印菜单上捕获click事件
如果您提供了针对此两个按钮单击事件尝试的要求,那么我们可能会为您提供备用解决方案.
小智 5
这很容易实现:
<body onafterprint="myFunction()">
Run Code Online (Sandbox Code Playgroud)
当完成打印作业或按下“取消”按钮时,可以在标签内定义的myFunction()将被触发。
| 归档时间: |
|
| 查看次数: |
24927 次 |
| 最近记录: |