首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用javascript在后台打印,即不弹出文档

使用javascript在后台打印,即不弹出文档
EN

Stack Overflow用户
提问于 2012-05-11 03:43:52
回答 2查看 2.2K关注 0票数 0

找到要从javascript打印的代码。但它会打开一个窗口,其中包含要打印的文档。有没有办法隐藏这份文件?

代码语言: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()上完成的,所以我猜没有它就不能打印。但它能被隐藏起来吗?

EN

回答 2

Stack Overflow用户

发布于 2012-05-11 03:55:37

您可以使用How to print only a selected HTML element?中描述的特定于打印的样式表来完成此操作。根本不要使用window.open();使用CSS类(如果需要,可以动态应用)来指定应该/不应该打印哪些元素。

票数 1
EN

Stack Overflow用户

发布于 2012-12-13 22:12:31

将以下内容添加到您的标记中:

代码语言:javascript
复制
<iframe id="ifrOutput" style="display:none;"></iframe>

添加以下javascript函数:

代码语言: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();
}

并这样称呼它:

代码语言:javascript
复制
// with stylesheet
printHtml('<div>Styled markup</div>', 'printStyles.css');

// without stylesheet
printHtml('<div>Unstyled markup</div>');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10541079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档