這裏是我的增強版本,當我們要加載CSS文件或存在於部分圖像參考打印。
在這些情況下,我們必須等到css文件或圖像完全加載後再調用print()函數。因此,我們最好將print()和close()函數調用移動到html中。以下是代碼示例:
var prtContent = document.getElementById("order-to-print");
var WinPrint = window.open('', '', 'left=0,top=0,width=384,height=900,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write('<html><head>');
WinPrint.document.write('<link rel="stylesheet" href="assets/css/print/normalize.css">');
WinPrint.document.write('<link rel="stylesheet" href="assets/css/print/receipt.css">');
WinPrint.document.write('</head><body onload="print();close();">');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.write('</body></html>');
WinPrint.document.close();
WinPrint.focus();
可能重複的[只打印
?](http://stackoverflow.com/questions/468881/print-div-id-printarea-div - 僅) –