简体   繁体   English

如何将CSS文件添加到print()

[英]How to add CSS file to print()

I've seen a couple answers to this but it doesn't seem to work for me. 我已经看到了几个答案,但似乎对我不起作用。 I have a function to print a grid, but the CSS doesn't transfer over ( unless I manually add in a style ). 我有一个打印网格的功能,但是CSS不会移过来(除非我手动添加样式)。 How do I add a CSS file? 如何添加CSS文件? I've tried: 我试过了:

$("#btn").click(function (e) {
        var divToPrint = document.getElementById("expertiseTable");
        newWin = window.open("");
        //var myStyle = '<link rel="stylesheet" type="text/css" href="http://localhost:59840/Content/Site.css" />';
        newWin.document.write('<html><head><link rel="stylesheet" type="text/css" href="http://localhost:59840/Content/Site.css" /></head><body onload="window.print()">' + divToPrint.innerHTML + '</body></html>');
        newWin.print();
});

Placing http://localhost:59840/Content/Site.css into the broswer brings up my CSS file so I know its there, but when I try to print it doesn't seem to find it. http:// localhost:59840 / Content / Site.css放到浏览器中会弹出我的CSS文件,因此我在那里知道它,但是当我尝试打印时似乎找不到它。

Did yout tried something like, 您是否尝试过类似的方法,

newWin.document.write('<html><head><link rel="stylesheet" type="text/css" href="http://localhost:59840/Content/Site.css" media="print" /></head><body onload="window.print()">' + divToPrint.innerHTML + '</body></html>');

Found here . 这里找到。

I placed the media="print" attribute. 我放置了media="print"属性。

There is also the option of the @media print usage, 还有@media print用法选项,

<style type="text/css">
    @media print{
        .aClass { display: none; }
    }
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM