JavaWeb开发,使用js调用打印机打印页面

首先,在html中,通过star和end来标记打印区域


1
2
3
4
5
6
7
8
< h1 >这块内容不需要打印</ h1 >
<!--startprint-->
< div class = "content" >
   这里是需要打印的内容
     .....
</ div >
<!--endprint-->
< h1 >这块内容不需要打印</ h1 >

然后,在点击事件中添加如下代码


1
2
3
4
5
6
7
8
9
function doPrint() {  
     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();  
}

猜你喜欢

转载自blog.csdn.net/javawutuobang/article/details/79075910