【发布时间】:2014-09-04 12:24:15
【问题描述】:
我正在尝试将 div 的内容打印到新选项卡,但是,它仅第一次打印空白页。当我手动打印页面时,它工作正常。我正在加载包含在 div 中的 2 个表。
HTML 结构:
<div class="receipt_bg" id="print_receipt">
<div id="div1">
table1
</div>
<div id="div2">
table2
</div>
</div>
<button type="button" id="print_btn">Print</button>
Javascript:
$('#print_btn').click(printClick);
function printClick(receipt_bg) {
var DocumentContainer = $('.receipt_bg').html();
var WindowObject = window.open("PrintWindow","_blank");
WindowObject.document.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">'+ '<html><head><title>test print receipt</title> <link href="../css/style.defaultprint.css" rel="stylesheet" type="text/css" /></head><body>' + DocumentContainer + '</body></html>')
setTimeout(WindowObject.print(), 5);
WindowObject.close();
}
我什至尝试使用 WindowObject.onload = WindowObject.print(); 代替 setTimeout(WindowObject.print(), 5); 它似乎仍然不起作用。我看到了一个类似的帖子,但它的解决方案对我不起作用。
【问题讨论】:
标签: javascript html printing new-window