0

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

<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 也集成到我的打印页面中呢?请帮我。

4

4 回答 4

2

找到linkCSS 的(或其他)并确保它具有media包含print.

于 2012-05-08T05:50:13.913 回答
2

尝试:

<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>
于 2012-05-08T05:57:44.410 回答
0

你可以试试这个:

<style type="text/css">
    @media print {
        #divtoprint { /** your css code goes here **/ }
        /** other selector style goes here which style you print **/
    }
</style>
于 2012-05-08T06:01:59.087 回答
0

您是否尝试过更改 CSS 标签以适用于所有媒体?

<link rel="stylesheet" href="style.css" media="all" type="text/css" />

或者如果你想更具体

<link rel="stylesheet" href="style.css" media="screen, print" type="text/css" />
于 2012-05-11T23:43:10.503 回答