打印 DIV 的内容

新手上路,请多包涵

打印 DIV 内容的最佳方法是什么?

原文由 usertest 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 526
2 个回答

与早期版本相比略有变化 - 在 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 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题