基于jQuery实现网页打印功能
作者:bea
直接上代码 <!DOCTYPE html><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>index</title></head><body> <div style="text-align:center; margin-top: 3
直接上代码
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>index</title>
</head>
<body>
<div style="text-align:center; margin-top: 30px">
<div id="printArea">
<div>......文本打印区域......</div>
<div>......文本打印区域......</div>
<div>......文本打印区域......</div>
<div>......文本打印区域......</div>
<div>......文本打印区域......</div>
</div>
<br>
<br>
<input id="btnPrint" type="button" value="打印文本区域"></input>
<input id="btnPrintFull" type="button" value="全屏打印"></input>
</div>
</body>
<script src="js/jquery-2.1.4.js"></script>
<script src="js/jquery.PrintArea.min.js"></script>
<script>
$("#btnPrint").click(function(){
$("#printArea").printArea();
});
$("#btnPrintFull").click(function(){
$("body").printArea();
});
</script>
</html>
代码 jquery.PrintArea.min.js:
(function(c){function g(){var b="<head><title>"+d.popTitle+"</title>";c(document).find("link").filter(function(){return"stylesheet"==c(this).attr("rel").toLowerCase()}).filter(function(){var a=c(this).attr("media");return void 0==a?!1:""==a.toLowerCase()||"print"==a.toLowerCase()}).each(function(){b+='<link type="text/css" rel="stylesheet" href="'+c(this).attr("href")+'" >'});return b+="</head>"}function h(b){return'<body><div class="'+c(b).attr("class")+'">'+c(b).html()+"</div></body>"}function k(b){c("input,select,textarea",b).each(function(){var a=c(this).attr("type");"radio"==a||"checkbox"==a?c(this).is(":not(:checked)")?this.removeAttribute("checked"):this.setAttribute("checked",!0):"text"==a?this.setAttribute("value",c(this).val()):"select-multiple"==a||"select-one"==a?c(this).find("option").each(function(){c(this).is(":not(:selected)")?this.removeAttribute("selected"):this.setAttribute("selected",!0)}):"textarea"==a&&(a=c(this).attr("value"),c.browser.mozilla?this.firstChild?this.firstChild.textContent=a:this.textContent=a:this.innerHTML=a)});return b}function l(){var b=d.id,a;try{a=document.createElement("iframe"),document.body.appendChild(a),c(a).attr({style:"border:0;position:absolute;width:0px;height:0px;left:0px;top:0px;",id:b,src:""}),a.doc=null,a.doc=a.contentDocument?a.contentDocument:a.contentWindow?a.contentWindow.document:a.document}catch(e){throw e+". iframes may not be supported in this browser.";}if(null==a.doc)throw"Cannot find document.";return a}function m(){var b;b="location=no,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no,width=595px,height=842px,top=0,left=0,toolbar=no,scrollbars=no,personalbar=no"+(",resizable=yes,screenX="+d.popX+",screenY="+d.popY+"");b=window.open("","_blank",b);b.doc=b.document;return b}var f=0,n={mode:"iframe",popHt:500,popWd:400,popX:200,popY:200,popTitle:"",popClose:!1},d={};c.fn.printArea=function(b){c.extend(d,n,b);f++;c("[id^=printArea_]").remove();b=k(c(this));d.id="printArea_"+f;var a,e;switch(d.mode){case"iframe":e=new l;a=e.doc;e=e.contentWindow||e;break;case"popup":e=new m,a=e.doc}a.open();a.write(("iframe"!=d.mode&&d.strict?'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01'+(0==d.strict?" Trasitional":"")+'//EN" "http://www.w3.org/TR/html4/'+(0==d.strict?"loose":"strict")+'.dtd">':"")+"<html>"+g()+h(b)+"</html>");a.close();e.focus();e.print();"popup"==d.mode&&d.popClose&&e.close()}})(jQuery);
二、效果图
打印文本区域:
全屏打印:
还有一些高手提出的window.print() 方法也是可以滴,在这就不一一介绍了,希望大家认真研究网页打印功能的实现方法,小编和大家共同进步。
有用 | 无用
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>index</title>
</head>
<body>
<div style="text-align:center; margin-top: 30px">
<div id="printArea">
<div>......文本打印区域......</div>
<div>......文本打印区域......</div>
<div>......文本打印区域......</div>
<div>......文本打印区域......</div>
<div>......文本打印区域......</div>
</div>
<br>
<br>
<input id="btnPrint" type="button" value="打印文本区域"></input>
<input id="btnPrintFull" type="button" value="全屏打印"></input>
</div>
</body>
<script src="js/jquery-2.1.4.js"></script>
<script src="js/jquery.PrintArea.min.js"></script>
<script>
$("#btnPrint").click(function(){
$("#printArea").printArea();
});
$("#btnPrintFull").click(function(){
$("body").printArea();
});
</script>
</html>
代码 jquery.PrintArea.min.js:
(function(c){function g(){var b="<head><title>"+d.popTitle+"</title>";c(document).find("link").filter(function(){return"stylesheet"==c(this).attr("rel").toLowerCase()}).filter(function(){var a=c(this).attr("media");return void 0==a?!1:""==a.toLowerCase()||"print"==a.toLowerCase()}).each(function(){b+='<link type="text/css" rel="stylesheet" href="'+c(this).attr("href")+'" >'});return b+="</head>"}function h(b){return'<body><div class="'+c(b).attr("class")+'">'+c(b).html()+"</div></body>"}function k(b){c("input,select,textarea",b).each(function(){var a=c(this).attr("type");"radio"==a||"checkbox"==a?c(this).is(":not(:checked)")?this.removeAttribute("checked"):this.setAttribute("checked",!0):"text"==a?this.setAttribute("value",c(this).val()):"select-multiple"==a||"select-one"==a?c(this).find("option").each(function(){c(this).is(":not(:selected)")?this.removeAttribute("selected"):this.setAttribute("selected",!0)}):"textarea"==a&&(a=c(this).attr("value"),c.browser.mozilla?this.firstChild?this.firstChild.textContent=a:this.textContent=a:this.innerHTML=a)});return b}function l(){var b=d.id,a;try{a=document.createElement("iframe"),document.body.appendChild(a),c(a).attr({style:"border:0;position:absolute;width:0px;height:0px;left:0px;top:0px;",id:b,src:""}),a.doc=null,a.doc=a.contentDocument?a.contentDocument:a.contentWindow?a.contentWindow.document:a.document}catch(e){throw e+". iframes may not be supported in this browser.";}if(null==a.doc)throw"Cannot find document.";return a}function m(){var b;b="location=no,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no,width=595px,height=842px,top=0,left=0,toolbar=no,scrollbars=no,personalbar=no"+(",resizable=yes,screenX="+d.popX+",screenY="+d.popY+"");b=window.open("","_blank",b);b.doc=b.document;return b}var f=0,n={mode:"iframe",popHt:500,popWd:400,popX:200,popY:200,popTitle:"",popClose:!1},d={};c.fn.printArea=function(b){c.extend(d,n,b);f++;c("[id^=printArea_]").remove();b=k(c(this));d.id="printArea_"+f;var a,e;switch(d.mode){case"iframe":e=new l;a=e.doc;e=e.contentWindow||e;break;case"popup":e=new m,a=e.doc}a.open();a.write(("iframe"!=d.mode&&d.strict?'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01'+(0==d.strict?" Trasitional":"")+'//EN" "http://www.w3.org/TR/html4/'+(0==d.strict?"loose":"strict")+'.dtd">':"")+"<html>"+g()+h(b)+"</html>");a.close();e.focus();e.print();"popup"==d.mode&&d.popClose&&e.close()}})(jQuery);
二、效果图
打印文本区域:
全屏打印:
还有一些高手提出的window.print() 方法也是可以滴,在这就不一一介绍了,希望大家认真研究网页打印功能的实现方法,小编和大家共同进步。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- jqGrid中文文档之选项设置
- javascript实现自动填写表单实例简析
- Jquery插件之Fancybox丰富的弹出层效果附源码下载
- 纯js代码实现简单计算器
- jquery判断输入密码两次是否相等
- 基于PHP和Mysql相结合使用jqGrid读取数据并显示
- Jqgrid之强大的表格插件应用
- 整理Javascript事件响应学习笔记
- jqGrid表格应用之新增与删除数据附源码下载
- 通过点击jqgrid表格弹出需要的表格数据
- 原生JavaScript实现异步多文件上传
- Jquery 1.9.1源码分析系列(十二)之筛选操作
- 日常收藏的jquery技巧
- JavaScript使用DeviceOne开发实战(三)仿微信应用
- jquery密码强度校验
- 基于jQuery实现的双11天猫拆红包抽奖效果
- jquery实现全选、反选、获得所有选中的checkbox
- js实现继承的5种方式
- 6种javascript显示当前系统时间代码