简体   繁体   English

如何使用Java脚本中的window.print方法更改页眉和页脚样式?

[英]How to change the header and footer style using the window.print method in java-script?

当我使用此window.print方法时,它显示了默认的页眉和页脚,我想在打印文档的页眉中添加一些样式和图像

You can use the print media query in your css like that: 您可以在CSS中使用印刷媒体查询,如下所示:

@media print {
 /* styles go here */
}

It is also applied if the user decides to print the page without window.print 如果用户决定不使用window.print打印页面,则也可以使用它。

For further methods of styling for print take a look at the @page rule. 有关打印样式的其他方法,请查看@page规则。 But keep in mind that browser support isn't great yet 但是请记住, 浏览器支持还不是很好

Example For custom Print 自定义打印示例

 function myFunction() { window.print(); } 
 .Print{ display:none !important; } .NoPrint{ display:block !important; } nav { margin-bottom:10px; border-bottom: solid 1px #000; } footer{ border-top: solid 1px #000; margin-top:10px; } footer , nav > a{ Font-size:30px; width:100%; text-align: center; } @media print { .Print{ display: block !important; } .NoPrint{ display: none !important; } } 
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav class="nav Print"> <a class="nav-link active" href="#">Print Logo</a> </nav> <nav class="nav NoPrint"> <a class="nav-link active" href="#">Active</a> </nav> <p>Click the button to print the current page.</p> <button onclick="myFunction()">Print this page</button> <footer class="Print"> Print copy rghit </footer> <footer class="NoPrint"> copy rghit </footer> 

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

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