找到要从javascript打印的代码。但它会打开一个窗口,其中包含要打印的文档。有没有办法隐藏这份文件?
var element=document.getElementById(element_id);
var newWin=window.open('','Print-Window','width=400,height=400,top=100,left=100');
newWin.document.open();
/* newWin.document.title = "Readings on PageLinks"; */
newWin.document.write('<html><head><title>Readings on PageLinks</title></head><body onload="window.print()">'+element.innerHTML+'</body></html>');
newWin.document.close();
setTimeout(function(){ newWin.close(); },10);打印是在该文档的onload()上完成的,所以我猜没有它就不能打印。但它能被隐藏起来吗?
发布于 2012-05-11 03:55:37
您可以使用How to print only a selected HTML element?中描述的特定于打印的样式表来完成此操作。根本不要使用window.open();使用CSS类(如果需要,可以动态应用)来指定应该/不应该打印哪些元素。
发布于 2012-12-13 22:12:31
将以下内容添加到您的标记中:
<iframe id="ifrOutput" style="display:none;"></iframe>添加以下javascript函数:
function printContainer(content, styleSheet) {
var output = document.getElementById("ifrOutput").contentWindow;
output.document.open();
if (styleSheet !== undefined) {
output.document.write('<link href="' + styleSheet + '" rel="stylesheet" type="text/css" />');
}
output.document.write(content);
output.document.close();
output.focus();
output.print();
}并这样称呼它:
// with stylesheet
printHtml('<div>Styled markup</div>', 'printStyles.css');
// without stylesheet
printHtml('<div>Unstyled markup</div>');https://stackoverflow.com/questions/10541079
复制相似问题