我一直在使用下面的代码打印我的页面:
window.print();
下面的图片是什么打印预览在Chrome浏览器看起来像。它有两个主要按钮:print
和cancel
。
我想知道用户是否单击了print
或cancel
按钮。我使用jquery:
打印预览的HTML代码:
<button class="print default" i18n-content="printButton">Print</button>
<button class="cancel" i18n-content="cancel">Cancel</button>
Jquery代码:
$('button > .cancel').click(function (e) {
alert('Cancel');
});
$('button > .print').click(function (e) {
alert('Print');
});
我试了上面的代码,没有运气。我错过了什么?
6条答案
按热度按时间6qfn3psc1#
您无法直接从常规网页访问Chrome的内部窗口(本例中为打印对话框)。
或者,如果您想在打开打印预览时执行某些操作,可以尝试以下操作:
参考:How to capture the click event on the default print menu called by Javascript window.print()
也许如果您提供您的要求,这两个按钮点击事件,我们可以为您提供一个替代的解决方案。
1sbrub3j2#
这是很容易做到:
当打印作业完成或按下取消按钮时,将触发可以在标记中定义的myFunction()。
g0czyy6m3#
j0pj023g4#
据我所知,打印预览不是你的JS可以访问的任何
document
的一部分。这些可能会让你感兴趣:Detecting browser print event
ExtJS 4 - detecting if the user pressed "Print" on the print dialog that was called programatically
xkrw2x1b5#
试试这个调整后的代码:
p1iqtdky6#
这个应该可以了我使用的是jQuery v2.2.0,它包含在html文件中。
我想这也可以用作在html中打印某些div的方法。只需要隐藏
body
元素,只显示你想用一些定位css打印的div。希望它在你的工作。我试过了,我可以说它对我有效。