js 客户端打印html 并且去掉页眉、页脚
print() 方法用于打印当前窗口的内容,支持部分或者整个网页打印。
调用 print() 方法所引发的行为就像用户单击浏览器的打印按钮。通常,这会产生一个对话框,让用户可以取消或定制打印请求。
win10下测试ie11、chrome、firefox、360、edge 都可以成功去掉页眉页脚;
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>打印</title> 5 <meta charset="utf-8"> 6 <style> 7 .printBox { 8 width: 300px; 9 height: 300px; 10 border: 1px solid blue; 11 } 12 </style> 13 <!-- 打印的样式--> 14 <style media="print"> 15 @page { 16 size: auto; 17 margin: 0mm; 18 } 19 </style> 20 </head> 21 22 <body> 23 <div class="printBox"> 24 this is content!!!<br> 25 点击按钮打印 26 </div> 27 <button onclick='print_page()'>打印</button> 28 </body> 29 30 <script type="text/javascript"> 31 function print_page() { 32 if (!!window.ActiveXObject || "ActiveXObject" in window) { //是否ie 33 remove_ie_header_and_footer(); 34 } 35 window.print(); 36 } 37 38 function remove_ie_header_and_footer() { 39 var hkey_path; 40 hkey_path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; 41 try { 42 var RegWsh = new ActiveXObject("WScript.Shell"); 43 RegWsh.RegWrite(hkey_path + "header", ""); 44 RegWsh.RegWrite(hkey_path + "footer", ""); 45 } catch (e) { 46 } 47 } 48 </script> 49 </html>
如果您觉得阅读本文对您有帮助,请点一下推荐按钮,您的推荐将是我最大的写作动力,欢迎各位转载!
分类:
Js
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)