首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在javascript中打印页面

在javascript中打印页面
EN

Stack Overflow用户
提问于 2012-05-08 13:48:24
回答 4查看 2.8K关注 0票数 0

我正在尝试用java脚本打印页面。代码如下:

代码语言:javascript
复制
<script type="text/javascript">
        function PrintContent(){
            var DocumentContainer = document.getElementById("divtoprint");
            var WindowObject = window.open("", "PrintWindow","width=1200,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();
        }
    </script>

然后,我调用了<a onclick="PrintContent();" style="cursor:pointer;"><img src="images/printer.png" alt="Print" title="printer"/></a>函数,也就是我想打印的内容,放在<div id="divtoprint"> </div>中。

但在本例中,我得到的是没有css样式的打印页面。那么如何将css集成到我的打印页面中呢?请帮帮我。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-05-08 13:57:44

尝试:

代码语言:javascript
复制
<script type="text/javascript">
    function PrintContent(){
    var DocumentContainer = document.getElementById("divtoprint");
    var WindowObject = window.open("", "PrintWindow","width=1200,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes");
    WindowObject.document.write('<html><head><title>Printwindow</title>');
    WindowObject.document.write('<link rel="stylesheet" href="style.css">');
    WindowObject.document.write('</head><body>');
    WindowObject.document.writeln(DocumentContainer.innerHTML);
    WindowObject.document.write('</body></html>');
    WindowObject.document.close();
    WindowObject.focus();
    WindowObject.print();
    WindowObject.close();
    }
</script>
票数 2
EN

Stack Overflow用户

发布于 2012-05-08 13:50:14

找到CSS的link (或其他任何内容),并确保它有一个包含printmedia属性。

票数 2
EN

Stack Overflow用户

发布于 2012-05-08 14:01:59

您可以尝试这样做:

代码语言:javascript
复制
<style type="text/css">
    @media print {
        #divtoprint { /** your css code goes here **/ }
        /** other selector style goes here which style you print **/
    }
</style>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10493215

复制
相关文章

相似问题

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