与早期版本相比略有变化 - 在 CHROME 上测试 function PrintElem(elem) { var mywindow = window.open('', 'PRINT', 'height=400,width=600'); mywindow.document.write('<html><head><title>' + document.title + '</title>'); mywindow.document.write('</head><body >'); mywindow.document.write('<h1>' + document.title + '</h1>'); mywindow.document.write(document.getElementById(elem).innerHTML); mywindow.document.write('</body></html>'); mywindow.document.close(); // necessary for IE >= 10 mywindow.focus(); // necessary for IE >= 10*/ mywindow.print(); mywindow.close(); return true; } 原文由 Bill Paetzke 发布,翻译遵循 CC BY-SA 3.0 许可协议
我认为有更好的解决方案。使您的 div 打印覆盖整个文档,但仅在打印时: @media print { .myDivToPrint { background-color: white; height: 100%; width: 100%; position: fixed; top: 0; left: 0; margin: 0; padding: 15px; font-size: 14px; line-height: 18px; } } 原文由 BC. 发布,翻译遵循 CC BY-SA 3.0 许可协议
与早期版本相比略有变化 - 在 CHROME 上测试