基于jQuery實(shí)現(xiàn)網(wǎng)頁打印功能
直接上代碼
<!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)頁打印功能的實(shí)現(xiàn)方法,小編和大家共同進(jìn)步。
相關(guān)文章
jquery修改屬性值實(shí)例代碼(設(shè)置屬性值)
jQuery attr()方法用于設(shè)置/改變屬性值,下面的例子演示如何改變(設(shè)置)鏈接中 href 屬性的值2014-01-01jQuery操作動(dòng)態(tài)生成的內(nèi)容的方法
這篇文章主要介紹了jQuery操作動(dòng)態(tài)生成的內(nèi)容的方法,對(duì)比分析了bind與live方法,說明了live方法在處理新增元素時(shí)的作用,需要的朋友可以參考下2016-05-05jQuery AJAX應(yīng)用實(shí)例總結(jié)
這篇文章主要介紹了jQuery AJAX應(yīng)用,結(jié)合實(shí)例形式總結(jié)分析了jQuery 使用AJAX訪問各種格式數(shù)據(jù)相關(guān)應(yīng)用操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-05-05Jquery 獲取表單text,areatext,radio,checkbox,select值的代碼
Jquery 獲取表單text,areatext,radio,checkbox,select值的實(shí)現(xiàn)代碼,學(xué)習(xí)jquery的朋友可以參考下。都是必須要了解的。2009-11-11js之ActiveX控件使用說明 new ActiveXObject()
ActiveX 控件廣泛用于Internet。它們可以通過提供視頻、動(dòng)畫內(nèi)容等來增加瀏覽的樂趣。不過,這些程序可能出問題或者向您提供不需要的內(nèi)容2014-03-03jquery 操作單選框,復(fù)選框,下拉列表實(shí)現(xiàn)代碼
jquery 操作單選框,復(fù)選框,下拉列表實(shí)現(xiàn)代碼,需要的朋友可以參考下。2009-10-10基于jquery實(shí)現(xiàn)百度新聞導(dǎo)航菜單滑動(dòng)動(dòng)畫
這篇文章主要介紹了基于jquery實(shí)現(xiàn)百度新聞導(dǎo)航菜單滑動(dòng)動(dòng)畫,感興趣的小伙伴們可以參考一下2016-03-03jQuery綁定事件-多種實(shí)現(xiàn)方式總結(jié)
下面小編就為大家?guī)硪黄猨Query綁定事件-多種實(shí)現(xiàn)方式總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05