单击按钮如何只打印一个组件。
我知道这个解决方案:
window.frames["print_frame"].window.focus(); window.frames["print_frame"].window.print(); $('.print_frame').remove();
但是React不想使用框架。
有什么办法吗?谢谢。
客户端上有两种解决方案。一种是像您张贴的框架。不过,您可以使用iframe:
var content = document.getElementById("divcontents"); var pri = document.getElementById("ifmcontentstoprint").contentWindow; pri.document.open(); pri.document.write(content.innerHTML); pri.document.close(); pri.focus(); pri.print();
这期望这个html存在
<iframe id="ifmcontentstoprint" style="height: 0px; width: 0px; position: absolute"></iframe>
另一种解决方案是使用媒体选择器,并在media="print"样式上隐藏所有您不想打印的内容。
media="print"
<style type="text/css" media="print"> .no-print { display: none; } </style>
最后一种方法需要在服务器上进行一些工作。您可以将所有HTML + CSS发送到服务器,并使用许多组件之一来生成可打印文档(例如PDF)。我已经尝试使用PhantomJs进行安装。