1、js实现打印,定义打印环境不显示不需要的部分
<!--打印的实现1:start-->
<input id="btnPrint" type="button" value="打印" onclick="javascript:window.print();" />
<!--将不需要打印的部分,标记为 class="noprint" -->
<style type="text/css" media=print>
.noprint{display : none }//定义打印环境不显示不需要的部分
</style>
<p class="noprint">不需要打印的地方</p>
<p>打印的部分</p>
<!--打印的实现1:end-->
2.以标签形式定义打印范围
<!--打印的实现2:start-->
<input id="btnPrint" type="button" value="打印预览" onclick=preview(1) />
<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
prnhtmlprnhtml=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-->
再加个打印按纽 onclick=preview(1)
<!--打印的实现2:end-->
2、调用widows底层打印(报安全警告,不建议使用)(这段代码包含去页眉页脚)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>中国绿色厨房计划</title>
<style type="text/css" media=print>
.noprint{display : none }
</style>
<script language=javascript>
function printsetup(){
// 打印页面设置
wb.execwb(8,1);
}
function printpreview(){
// 打印页面预览
wb.execwb(7,1);
}
function printit(){
if (confirm(确定打印吗?)){
wb.ExecWB(6,1)
//wb.execwb(1,1)//打开
//wb.ExecWB(2,1);//关闭现在所有的IE窗口,并打开一个新窗口
//wb.ExecWB(4,1)//;保存网页
//wb.ExecWB(6,1)//打印
//wb.ExecWB(7,1)//打印预览
//wb.ExecWB(8,1)//打印页面设置
//wb.ExecWB(10,1)//查看页面属性
//wb.ExecWB(15,1)//好像是撤销,有待确认
//wb.ExecWB(17,1)//全选
//wb.ExecWB(22,1)//刷新
//wb.ExecWB(45,1)//关闭窗体无提示
}
}
</script>
<script>
2 var HKEY_Root,HKEY_Path,HKEY_Key;
3 HKEY_Root="HKEY_CURRENT_USER";
4 HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
5 //设置网页打印的页眉页脚为空
6 function PageSetup_Null()
7 {
8 try
9 {
10 var Wsh=new ActiveXObject("WScript.Shell");
11 HKEY_Key="header";
12 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
13 HKEY_Key="footer";
14 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
15 }
16 catch(e)
17 {
18
19 }
20 }
21 //设置网页打印的页眉页脚为默认值
22 function PageSetup_Default()
23 {
24 try
25 {
26 var Wsh=new ActiveXObject("WScript.Shell");
27 HKEY_Key="header";
28 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b页码,&p/&P");
29 HKEY_Key="footer";
30 Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&u&b&d");
31 }
32 catch(e)
33 {}
34 }
35
36 </script>
</head>
<body>
<div class="noprint" style="width:640px;height:20px;margin:100px auto 0 auto;font-size:12px;text-align:right;">
<input value="打印" type="button" onclick="javascript:window.print()" />
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" id="wb" name="wb" width="0">
</OBJECT>
<input type=button name=button_print style="display:none;" value="打印本单据" onclick="javascript:printit()">
<input type=button name=button_setup value="打印页面设置" onclick="javascript:printsetup();">
<input type=button name=button_show value="打印预览" onclick="javascript:printpreview();">
<input type=button name=button_fh value="关闭" onclick="javascript:window.close();">
</div>
<div style="width:640px;height:624px;margin:20px auto;">打印内容</div>
3、jq打印插件
<script language="javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jquery.PrintArea.js"></script>
<div class="my_show">
这个是打印时显示的。
</div>
<div class="my_hidden">
这个是打印时隐藏的。
</div>
<input type="button" id="print"/>
<script>
$(document).ready(function(){
$("#print").click(function(){
$(".my_show").printArea();
});
});
</script>
参数可配置,使用的方法:$(element).printArea(option).参数设置:1.mode:模式,当点击打印按钮时触发模式,默认为iframe,当设置为popup则会新开一个窗口页面打印。2.popTitle:设置新开窗口的标题,默认为空。3.popClose:完成打印后是否关闭窗口,默认为false。

共有条评论 网友评论