我試圖將div的內容打印到新選項卡,但是,它僅在第一次打印空白頁。當我手動打印頁面時,它工作正常。我正在加載2個包裝在一個div中的表格。在新選項卡中打印內容打印空白頁
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); 它似乎還沒有工作。我看到了類似的帖子,但其解決方案對我而言並不適用。
如果沒有使用任何插件,我可以不正確嗎?我嘗試將時間從50增加到500甚至1000,但仍然沒有運氣。 – ekta 2014-09-10 19:44:58
@ekta:你有答案嗎,還是我幫你進一步? – bharatpatel 2014-09-12 09:30:32
我能夠通過使用某個插件來獲得此權限,但我不想這樣做。你能不能使用插件來幫助我?我試着增加setTimeout()中的時間。 – ekta 2014-09-24 19:59:51