【问题标题】:Print A Div Containing Dynamic Image打印包含动态图像的 div
【发布时间】:2015-12-24 17:16:05
【问题描述】:

我使用以下代码在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 时,图像没有显示,它消失了。

有什么提示吗?

【问题讨论】:

  • 你不应该在关闭document之前发出print命令吗?
  • 没什么变化...:)
  • div 存在于当前页面,不在您打开打印的新窗口中。而print() 只是打开打印对话框。
  • 在发出打印命令之前尝试等待图像加载
  • @WhiteHat: 但是我把 div 的内容放到我要打开的窗口中...

标签: javascript jquery html printing


【解决方案1】:

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

因此,改为在您的函数中打印尝试在弹出窗口内的窗口onload 事件上打印,例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多