在 JavaScript 中打印页面
page print in javascript
本文关键字:打印 JavaScript 更新时间:2023-09-26
我正在尝试通过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>
找到 CSS 的link
(或其他任何内容),并确保它具有包含 print
的 media
属性
你可以试试这个:
<style type="text/css">
@media print {
#divtoprint { /** your css code goes here **/ }
/** other selector style goes here which style you print **/
}
</style>
您是否尝试过更改 CSS 标签以适用于所有媒体?
<link rel="stylesheet" href="style.css" media="all" type="text/css" />
或者如果你想更具体
<link rel="stylesheet" href="style.css" media="screen, print" type="text/css" />
相关文章:
- 打印javascript控制台的堆栈跟踪
- php:打印javascript/jquery dom就绪
- 如何在html元素中打印javascript变量
- 在htm-php页面中打印Javascript函数
- 使用请求对象中可用的值打印 JavaScript 警报
- 如何使用PHP打印JavaScript
- 如何在我的视图中打印JavaScript代码
- 在自定义控制台中打印javascript错误
- 使用POS打印机打印Javascript收据
- 在移动设备(ios)上打印Javascript
- 如果会话中的属性不为 null 或默认值,则运行/打印 JavaScript Razor ASP.NET MVC
- 在 PHP 中打印 JavaScript 代码不起作用
- 在 php 中打印 javascript 的值
- 打印 Javascript 而不是 ASP(经典)输出
- 如何在 HTML 中打印 javascript 函数的返回值 “添加 {x} +{y}={result}”
- 在 PHP 中打印 JavaScript 代码
- WebClientPrint 用于 Chrome 上的客户端打印 javascript 问题
- 如何打印 JavaScript 非对象的所有函数
- PHP 字符串包含打印 Javascript 时的换行符问题
- 在 Javascript 中打印 Javascript