window.print()取消按钮回调

前景提要

要实现打印功能,并且按钮不需要打印。当直接使用打印前将按钮隐藏,点击取消打印时,按钮将不会出现。在这里插入图片描述

如何实现

//beforePrint  打印前将按钮隐藏
afterPrint  取消时将按钮回显
其他内容可以完全复制,就可以用
 var beforePrint = function() {
    
    
        $("#exportZghfd").hide()
        $("#dyZghfd").hide()
        $("#from_btns").hide()

    };

    var afterPrint = function() {
    
    
        $("#exportZghfd").show()
        $("#dyZghfd").show()
        $("#from_btns").show()
    };

    if (window.matchMedia) {
    
       
        var mediaQueryList = window.matchMedia('print');
        mediaQueryList.addListener(function(mql) {
    
    
            if (mql.matches) {
    
    
                beforePrint();
            } else {
    
    
                afterPrint();
            }
        });
    }

    window.onbeforeprint = beforePrint;
    window.onafterprint = afterPrint;
    
    $("#dyZghfd").click(function () {
    
    
        window.print();
    })

猜你喜欢

转载自blog.csdn.net/gradonisis/article/details/125800148