在打印过程中隐藏元素,并在打印完成后显示



http://www.highcharts.com/demo此链接包含 jquery 图表。图表演示的右上角有一个打印选项。它在打印过程中隐藏元素,这很容易做到,但它在打印后显示所有元素。这意味着它们会检测打印是否完成。如何在我自己的网页中实现这种打印技术?

有javascript事件。正如您在此代码中看到的,取自:http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/

(function() {
    var beforePrint = function() {
        console.log('Functionality to run before printing.');
    };
    var afterPrint = function() {
        console.log('Functionality to run after printing');
    };
if (window.matchMedia) {
    var mediaQueryList = window.matchMedia('print');
    mediaQueryList.addListener(function(mql) {
        if (mql.matches) {
            beforePrint();
        } else {
            afterPrint();
        }
    });
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
}());

window.onbeforeprint 和 window.onafterprint 在 IE5+ 和 Firefox6+ 中受支持,但对于 Webkit 浏览器,有一个解决方法是 window.matchMedia。

不过,此解决方案不适用于Opera,仅适用于Firefox,IE和WebKit浏览器。

最新更新