打印包含动态图像的Div

Print A Div Containing Dynamic Image

本文关键字:Div 图像 动态 包含 打印      更新时间:2023-09-26

我使用以下代码在javascript中打印div

 $('body').on('click','button#printreceipt',function(){
        var printWindow = window.open('', '', 'height=400,width=800');
        var divContents = $("#appendReceiptHere").html();
        printWindow.document.write(divContents);
        printWindow.print();
        printWindow.document.close();
    });

div包含动态生成的图像,如

<img src="/mysite/generate_test_image.php" />

div正确显示,动态图像也正确显示。但是当我尝试打印div时,图像没有显示,它消失了。

有什么提示吗?

尝试等待图像加载,然后打印。

因此,要在函数中打印,请尝试在弹出窗口内的窗口onload事件上打印,如:

$('body').on('click','button#printreceive',function(){var printWindow=window.open('','','height=400,width=800');vardivContents=$("#appendReceiptHere").html()+"<脚本>"+"window.onload=函数(){"+"window.print();"+"};"+"<"+"/script>";printWindow.docent.write(divContents);printWindow.dococument.close();});