php - 在javascript中打印页面

标签 php javascript css printing

我正在尝试通过 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 也集成到我的打印页面中呢?请帮助我。

最佳答案

尝试:

<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>

关于php - 在javascript中打印页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10493215/

相关文章:

PHP 事件日历

php - lastInsertId 在 Postgresql 中不起作用

javascript - 向一个元素添加多个事件监听器

html - 如何在标题图像顶部制作带有文本和 Logo 的标题图像?

jquery - 仅为一组匹配项中的第一个 div 添加属性

选择元素的 PHP 版本/IE9 CSS 冲突

php - 加载数据文件中每行跳过 1 行

javascript - `if (console == undefined)` 怎么会中断 js 执行?

Javascript 从网络服务返回文件

javascript - Magento 菜单上的自定义 javascript