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

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

1、js实现(可实现局部打印)

<html> 
<title>js打印</title> 
<head></head><body> 
<input id="btnPrint" type="button" value="打印" onclick="javascript:window.print();" /> 
<input id="btnPrint" type="button" value="打印预览" onclick=preview(1) /> 
<style type="text/css" media=print> 
.noprint{display : none } 
</style> 

<p class="noprint">不需要打印的地方</p> 

<script> 
function preview(oper) 
{ 
if (oper < 10) 
{ 
bdhtml=window.document.body.innerHTML;//获取当前页的html代码 
sprnstr="<!--startprint"+oper+"-->";//设置打印开始区域 
eprnstr="<!--endprint"+oper+"-->";//设置打印结束区域 
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //从开始代码向后取html 

prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html 
window.document.body.innerHTML=prnhtml; 
window.print(); 
window.document.body.innerHTML=bdhtml; 
} else { 
window.print(); 
} 
} 
</script> 
<p>XXXXX</p> 
<!--startprint1-->要打印的内容<!--endprint1--> 
</body> 
</html> 

2、调用windows底层打印,报安全警告,不建议使用(不支持局部打印)

<HTML> 
<HEAD> 
<TITLE>javascript打印-打印页面设置-打印预览代码</TITLE> 
<META http-equiv=Content-Type content="text/html; charset=gb2312" /> 
<SCRIPT language=javascript> 
  function printsetup(){ 
  // 打印页面设置 
  wb.execwb(8,1); 
  } 
  function printpreview(){ 
  // 打印页面预览 
     
  wb.execwb(7,1);       
     
  } 

  function printit() 
  { 
  if (confirm('确定打印吗?')) { 
  wb.execwb(6,6); 
  } 
  } 
  </SCRIPT> 
</HEAD> 
<BODY> 

<DIV align=center> 
<OBJECT id=wb height=0 width=0 
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 name=wb></OBJECT> 
<INPUT onclick=javascript:printit() type=button value=打印 name=button_print /> 
<INPUT onclick=javascript:printsetup(); type=button value=打印页面设置 name=button_setup /> 
<INPUT onclick=javascript:printpreview(); type=button value=打印预览 name=button_show /> 
一按开始的减肥了卡时间段 
</DIV> 
</BODY> 
</HTML>

3、jQuery实现(支持局部打印)

<html> 
<head> 
<script type="text/javascript" src="jquery-1.6.4.js"></script> 
<script> 
$(function(){ 
$("input#biuuu_button").click(function(){ 
$("div#myPrintArea").printArea(); 
}); 
</script> 
</head> 
<body> 
<input id="biuuu_button" type="button" value="打印"></input> 
<div id="myPrintArea">.....文本打印部分.....</div> <div class="quote_title">引用</div><div class="quote_div"></div> 
</body> 
</html>

上面的预览和打印按钮不希望打印,如果要过滤的话可以做下面的样式设置

<style type="text/css">
  @media print {
    .noprint{
      display: none;
    }
  }
</style>

or

<style type="text/css" media="print">
  .noprint{
    display: none;
  }
</style>

两种写法任选其一

分页打印

使用 window.print() 打印时,如果内容超出会自动分页。但是我们如果需要自定义分页范围,如碰到表格分页打印,可以通过进行如下设置:

<table width="100%" border="0" cellpadding="0" cellspacing="0" style="page-break-after:always" > 
</table>

方式二、jqprint()

jqprint是一个基于jQuery编写的页面打印的一个小插件,但是不得不承认这个插件确实很厉害,最近的项目中帮了我的大忙,在Web打印的方面,前端的打印基本是靠window.print()的方式进行打印的,而这个插件在其基础上进行了进一步的封装,可以轻松实现打印网页上的某个区域,这是个亮点。

参考网址:http://www.jb51.net/article/102230.htm

请注意!很多朋友遇到 Cannot read property ‘opera’ of undefined 错误问题是juqery版本兼容问题

解决方法:加入迁移辅助插件 jquery-migrate-1.0.0.js可解决版本问题

引入

<script language="javascript" src="jquery-1.4.4.min.js"></script>
<script language="javascript" src="jquery.jqprint-0.3.js"></script>

js

<script language="javascript">
function a(){
    $("#ddd").jqprint();
  }
</script>

html

<div id="ddd">
  <table>
    <tr>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
      <td>test</td>
    </tr>
  </table>
</div>
<input type="button" onclick=" a()" value="打印"/>

设置模板打印

$("#printContainer").jqprint({
   debug: false, //如果是true则可以显示iframe查看效果(iframe默认高和宽都很小,可以再源码中调大),默认是false
   importCSS: true, //true表示引进原来的页面的css,默认是true。(如果是true,先会找$("link[media=print]"),若没有会去找$("link")中的css文件)
   printContainer: true, //表示如果原来选择的对象必须被纳入打印(注意:设置为false可能会打破你的CSS规则)。
   operaSupport: true//表示如果插件也必须支持歌opera浏览器,在这种情况下,它提供了建立一个临时的打印选项卡。默认是true
});

未经允许不得转载:哈勃私语 » javascript或jquery实现页面打印可局部打印

本文共3337个字 创建时间:2017年11月22日21:07   

分享到:更多 ()