<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>打印功能</title> <style> h1{ text-align: center; } #main { width: 800px; height: 200px; margin: 0 auto; border: 1px solid #ccc; } p { text-align: center; } span { display: inline-block; width: 100px; height: 40px; background: #3385FF; text-align: center; line-height: 40px; color: #fff; float: right; margin-top: 20px; margin-right: 320px; cursor: pointer; } </style> </head> <body> <!--startprint--> <div id="main"> <h1>xx报告</h1> <p>打印标题</p> <p>打印内容</p> <p>正文部分正文部分正文部分正文部分正文部分正文部分正文部分</p> </div> <!--endprint--> <span id="printBtn" onclick="preview()">打印</span> <script> //打印 function preview() { bdhtml=window.document.body.innerHTML; sprnstr="<!--startprint-->"; eprnstr="<!--endprint-->"; prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); window.document.body.innerHTML=prnhtml; window.print(); // 返回原界面 window.document.body.innerHTML = bdhtml window.location.reload() } </script> </body> </html>
上一篇:前端技术经验总结