网页打印(局部打印)

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3     <head>
 4         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5         <title>无标题文档</title>
 6         <link rel="stylesheet" type="text/css" href="resources/themes/main/index.css" />
 7     </head>
 8 
 9     <body>
10         <div style="overflow:auto; height:100%;">
11             <!--startprint1-->
12             <div class="bdtitle">xxx办文单</div>
13             <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="bdtab">
14                 <tr>
15                     <td class="fwtitle" width="5%">文别</td>
16                     <td>请示</td>
17                     <td class="fwtitle">密级</td>
18                     <td></td>
19                     <td class="fwtitle">缓急</td>
20                     <td></td>
21                     <td colspan="2">9号</td>
22                 </tr>
23                 <tr>
24                     <td class="fwtitle">备注</td>
25                     <td colspan="7"></td>
26                 </tr>
27             </table>
28             <div style="text-align: right;" class="bdtitle"> 年      月      日印发   共10份</div>
29             <!--endprint1-->
30             <div class="buttomdiv">
31                 <input type="submit" name="Submit2" value="打印" class="ssinput" onclick="preview(1)" />
32                 <input type="submit" name="Submit2" value="关闭" class="ssinput" />
33             </div>
34         </div>
35         <script>
36         function preview(oper) {
37             if (oper < 10) {
38                 bdhtml = window.document.body.innerHTML;//获取当前页的html代码
39                 sprnstr = "<!--startprint" + oper + "-->";//设置打印开始区域
40                 eprnstr = "<!--endprint" + oper + "-->";//设置打印结束区域
41                 prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 18); //从开始代码向后取html
42 
43                 prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));//从结束代码向前取html
44                 window.document.body.innerHTML = prnhtml;
45                 window.print();
46                 window.document.body.innerHTML = bdhtml;
47             } else {
48                 window.print();
49             }
50             
51         }
52     </script>
53     </body>
54 
55 </html>
原文地址:https://www.cnblogs.com/yanjc/p/5441804.html