gpt4 book ai didi

javascript - 打印包含动态图像的 div

转载 作者:行者123 更新时间:2023-11-30 16:23:10 25 4
gpt4 key购买 nike

我使用以下代码在 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#printreceipt',function(){   var printWindow = window.open('', '', 'height=400,width=800');   var divContents = $("#appendReceiptHere").html() +                        "<script>" +                        "window.onload = function() {" +                        "     window.print();" +                        "};" +                        "<" + "/script>";   printWindow.document.write(divContents);   printWindow.document.close();});

关于javascript - 打印包含动态图像的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34455981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com