javascript - 将 CSS 应用于 printWindow.print();在 JavaScript 中

标签 javascript html twitter-bootstrap css

我需要打印 HTML 页面中的一个部分。我使用下面的JS方法来打印它。

printWindow.print();

我就是这么做的。

$("#toprint").on("click", function () {

            var divContents = $("#tobebilled").html();
            var printWindow = window.open('', '', 'height=400,width=800');

printWindow.document.write('<link rel="stylesheet" type="text/css" href="style/print.css"/>'); 
            printWindow.document.write('<html><head><title>Print Bill</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(divContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();

问题是我使用了 3 个 css 文件

Custom.min.css
bootstrap.min.css
components.min.css

在我的打印输出中,我似乎无法应用这些样式。打印出来的是原始的。如何应用 CSS?

最佳答案

printWindow.document.write('<link rel="stylesheet" type="text/css" href="style/print.css"/>')

这包括 print.css。

对其他 css 文件执行相同的操作,并使用正确的文件路径

关于javascript - 将 CSS 应用于 printWindow.print();在 JavaScript 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37318538/

相关文章:

javascript - 在 JavaScript 中链接 'bind' 和 'call'?

javascript - 如何使用 React.js 调用从 Grid 读取的 kendo ui 数据源

javascript - 我可以使用 document.getElementById(someid).onclick 标记 a

javascript - 如何将异步方法拆分成更小的部分并依次调用它们?

html - 将微调器置于按钮中心

jquery - Bootstrap 崩溃。如何一次只展开一个div

python - 使用 Flask/Jinja2 宏设置事件导航元素的样式

html - 带选择选项的多级导航列表

html - 为按钮的旋转添加延迟

javascript - Bootstrap 滑动菜单 navbar-right