基于jQuery實(shí)現(xiàn)網(wǎng)頁(yè)打印功能
直接上代碼
<!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>......文本打印區(qū)域......</div>
<div>......文本打印區(qū)域......</div>
<div>......文本打印區(qū)域......</div>
<div>......文本打印區(qū)域......</div>
<div>......文本打印區(qū)域......</div>
</div>
<br>
<br>
<input id="btnPrint" type="button" value="打印文本區(qū)域"></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);
二、效果圖
打印文本區(qū)域:

全屏打?。?/p>

還有一些高手提出的window.print() 方法也是可以滴,在這就不一一介紹了,希望大家認(rèn)真研究網(wǎng)頁(yè)打印功能的實(shí)現(xiàn)方法,小編和大家共同進(jìn)步。
相關(guān)文章
jQuery實(shí)現(xiàn)邊框動(dòng)態(tài)效果的實(shí)例代碼
這篇文章給大家分享了一個(gè)jQuery邊框動(dòng)態(tài)的效果,當(dāng)鼠標(biāo)移動(dòng)到邊框區(qū)域的時(shí)候,邊框會(huì)有個(gè)動(dòng)態(tài)的加載動(dòng)畫(huà)效果,實(shí)現(xiàn)的效果真的非常不錯(cuò),下面來(lái)一起看看吧。2016-09-09
jquery實(shí)現(xiàn)手風(fēng)琴展開(kāi)效果
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)手風(fēng)琴展開(kāi)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
jquery實(shí)現(xiàn)二級(jí)導(dǎo)航下拉菜單效果
這篇文章主要介紹了jquery實(shí)現(xiàn)二級(jí)導(dǎo)航下拉菜單效果,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
15個(gè)款優(yōu)秀的 jQuery 圖片特效插件推薦
這篇文章向大家推薦15個(gè)最佳 jQuery 圖片效果插件。jQuery是最流行的Javascript框架,使用簡(jiǎn)單靈活,同時(shí)還有許多成熟的插件可供選擇2011-11-11
jQuery插件zTree實(shí)現(xiàn)的多選樹(shù)效果示例
這篇文章主要介紹了jQuery插件zTree實(shí)現(xiàn)的多選樹(shù)效果,結(jié)合實(shí)例形式分析了jQuery樹(shù)形插件zTree實(shí)現(xiàn)多選樹(shù)效果的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-03-03
Jquery Ajax的Get方式時(shí)需要注意URL地方
我們要時(shí)刻注意瀏覽器緩存, 當(dāng)使用GET方式時(shí)要添加時(shí)間戳參數(shù) (net Date()).getTime() 來(lái)保證每次發(fā)送的URL不同, 可以避免瀏覽器緩存.2011-04-04
ASP.NET中AJAX 調(diào)用實(shí)例代碼
最近在ASP.NET中做了一個(gè)AJAX調(diào)用 : Client端先從ASP.NET Server后臺(tái)取到一個(gè)頁(yè)面模板,然后在頁(yè)面初始化時(shí)再?gòu)腟erver中取一些相關(guān)數(shù)據(jù)以實(shí)現(xiàn)頁(yè)面模板的動(dòng)態(tài)顯示2012-05-05

