在Javascript中没有POP-Up窗口的情况下打印div标签的内容 [英] Printing the contents of a div tag without a POP-Up window in Javascript
本文介绍了在Javascript中没有POP-Up窗口的情况下打印div标签的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力在没有弹出窗口的情况下打印div标签的内容
I'm struggling to print the contents of a div tag without a pop up window
此刻我的代码是这样的
var DocumentContainer = document.getElementById('print');
var WindowObject = window.open('', 'Completed Registration Form', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes');
WindowObject.document.writeln(DocumentContainer.innerHTML);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
WindowObject.close();
下面使用弹出窗口,有没有办法不使用弹出窗口来做到这一点.
The below uses a popup window, is there a way to not do this using a popup window.
推荐答案
还有一种更有效的方法-没有jQuery或任何其他库
There is a more efficient way - without jQuery or any other library
这个想法是将窗口替换为隐藏的iframe,然后打印iframe的内容.
The idea is to replace the window with a hidden iframe, and print the contents of the iframe.
这是该代码:
function ClickHereToPrint(){
try{
var oIframe = document.getElementById('ifrmPrint');
var oContent = document.getElementById('divToPrint').innerHTML;
var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
if (oDoc.document) oDoc = oDoc.document;
oDoc.write('<head><title>title</title>');
oDoc.write('</head><body onload="this.focus(); this.print();">');
oDoc.write(oContent + '</body>');
oDoc.close();
} catch(e){
self.print();
}
}
这认为您的页面中有一个iframe.如果不这样做,只需使用以下命令创建一个:document.createElement('iframe')
This considers that you have an iframe in your page. if you don't, just create one using: document.createElement('iframe')
这篇关于在Javascript中没有POP-Up窗口的情况下打印div标签的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文