Recently, a friend asked how to print preview on js. Today I will explain it. First, I will understand the printing principle. In fact, printing a partial page is very simple. It is to use the starting mark to print the part you need to print. As for how to write the mark, whatever you want. I'll write <!--startprint--> what needs to be printed
<!--endprint-->. Because the marking does not need to be seen by the user, it is commented! The specific implementation code is as follows:
<!DOCTYPE html> <html> <head> <title>Simple implementation of printing preview</title> </head> <body> <div> This is the content in the body that does not need to be printed. The specific page design is designed according to your own requirements. If you need multiple tags on a page, you can generate tags dynamically </div> <!--startprint--> <div> This is what I need to print</div> <!--endprint--> <script type="text/javascript"> function preview() { var bdhtml=window.document.body.innerHTML;//Get the html code of the current page var startStr="<!--startprint-->";//Set the print start area var endStr="<!--endprint-->";//Set the print end area var printHtml=bdhtml.substring(bdhtml.indexOf(startStr)+startStr.length,bdhtml.indexOf(endStr));//Get the page to be printed from the tag window.document.body.innerHTML=printHtml;//Page to be printed window.print(); window.document.body.innerHTML=bdhtml;//Restore the interface} preview(); </script> </body> </html>