我想使用 JavaScript 打印网页。但我不想将页面作为弹出窗口打开。如何使用 JavaScript window.print 方法直接打印像“mypage.aspx”这样的网页而不将其作为弹出窗口打开?
条件也是“我不想为此使用任何 ActiveX”
这是我想尝试的:
var printWindow, printData; printWindow = window.open("", "printVersion", "menubar,scrollbars,width=640,height=480,top=0,left=0");
printData=document.getElementById("lblReport").innerHTML; printWindow.document.write(printData);
printWindow.document.close();
printWindow.print();
最佳答案
最简单的解决方案是将 mypage.aspx 的内容加载到 iframe,然后在 iframe 的 onload 事件上调用 window.print。
<button onclick="printPage()">print</button>
<div id="printerDiv" style="display:none"></div>
<script>
function printPage()
{
var div = document.getElementById("printerDiv");
div.innerHTML = '<iframe src="mypage.aspx" onload="this.contentWindow.print();"></iframe>';
}
</script>
关于asp.net - 如何在不打开弹出窗口的情况下打印网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2651559/