js或jquery实现页面打印可局部打印

js或jquery实现页面打印(局部打印) 

1、js实现(可实现局部打印) 
代码如下:
[html]  view plain  copy
 print ?
  1. <html>  
  2. <title>js打印</title>  
  3. <head></head><body>  
  4. <input id="btnPrint" type="button" value="打印" onclick="javascript:window.print();" />  
  5.   
  6. <input id="btnPrint" type="button" value="打印预览" onclick=preview(1) />  
  7. <style type="text/css" media=print>  
  8. .noprint{display : none }  
  9. </style>  
  10.   
  11.   
  12. <p class="noprint">不需要打印的地方</p>  
  13.   
  14. <script>  
  15. function preview(oper)  
  16. {  
  17. if (oper < 10)  
  18. {  
  19. bdhtml=window.document.body.innerHTML;//获取当前页的html代码  
  20. sprnstr="<!--startprint"+oper+"-->";//设置打印开始区域  
  21. eprnstr="<!--endprint"+oper+"-->";//设置打印结束区域  
  22. prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //从开始代码向后取html  
  23.   
  24. prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html  
  25. window.document.body.innerHTML=prnhtml;  
  26. window.print();  
  27. window.document.body.innerHTML=bdhtml;  
  28. } else {  
  29. window.print();  
  30. }  
  31. }  
  32. </script>  
  33. <p>XXXXX</p>  
  34. <!--startprint1-->要打印的内容<!--endprint1-->  
  35. </body>  
  36. </html>   
2、jQuery实现(支持局部打印) 
代码如下:
[html]  view plain  copy
 print ?
  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="jquery-1.6.4.js"></script>  
  4. <script>  
  5. $(function(){  
  6. $("input#biuuu_button").click(function(){  
  7. $("div#myPrintArea").printArea();  
  8. });  
  9. </script>  
  10. </head>  
  11. <body>  
  12. <input id="biuuu_button" type="button" value="打印"></input>  
  13. <div id="myPrintArea">.....文本打印部分.....</div> <div class="quote_title">引用</div><div class="quote_div"></div>  
  14. </body>  
  15. </html>  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值