使用方法介绍:
CSS控制页面打印区域
使用CSS控制打印样式,需要设置样式media="print",并且将页面中不需要打印的元素的样式display属性设置为none。如DEMO中,我将页头页脚及其他不需要打印的元素的样式设置如下:
<style type="text/css" media="print"> #header,.top_title,#jqprint,#footer,#cssprint h3{display:none} </style>jQuery控制页面打印区域
这里我要给大家介绍一个jQuery打印插件printArea.js。该插件使用简单,可以实现控制打印页面中指定的区域。
DEMO中有这样一段代码:
<p><a href="#" id="print_btn">点击这里打印>></a></p> <div id="my_area"> ...打印区域...<br/> </div>我们需要打印ID为my_area的DIV区域,只需要加入以下代码:
$(function(){ $("#print_btn").click(function(){ $("#my_area").printArea(); }); });当单击打印按钮时,调用件printArea.js插件。该插件还提供了一些参数可配置,使用方法:$(element).printArea(option)。
参数设置:
1、mode:模式,当点击打印按钮时触发模式,默认为iframe,当设置为popup则会新开一个窗口页面打印。
2、popTitle:设置新开窗口的标题,默认为空。
3、popClose:完成打印后是否关闭窗口,默认为false。
PS:IE浏览器打印页面取出页眉页脚网址的方法:文件->页面设置,将页面和页脚的输入框清空即可。
插件源代码:
(function($) { var counter = 0; var modes = { iframe : "iframe", popup : "popup" }; var defaults = { mode : modes.iframe, popHt : 500, popWd : 400, popX : 200, popY : 200, popTitle : '', popClose : false };
var settings = {};//global settings
$.fn.printArea = function( options ) { $.extend( settings, defaults, options );
counter++; var idPrefix = "printArea_"; $( "[id^=" + idPrefix + "]" ).remove(); var ele = getFormData( $(this) );
settings.id = idPrefix + counter;
var writeDoc; var printWindow;
switch ( settings.mode ) { case modes.iframe : var f = new Iframe(); writeDoc = f.doc; printWindow = f.contentWindow || f; break; case modes.popup : printWindow = new Popup(); writeDoc = printWindow.doc; }
writeDoc.open(); writeDoc.write( docType() + "<html>" + getHead() + getBody(ele) + "</html>" ); writeDoc.close();
printWindow.focus(); printWindow.print();
if ( settings.mode == modes.popup && settings.popClose ) printWindow.close(); }
function docType() { if ( settings.mode == modes.iframe || !settings.strict ) return "";
var standard = settings.strict == false ? " Trasitional" : ""; var dtd = settings.strict == false ? "loose" : "strict";
return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01' + standard + '//EN" "http://www.w3.org/TR/html4/' + dtd + '.dtd">'; }
function getHead() { var head = "<head><title>" + settings.popTitle + "</title>"; $(document).find("link") .filter(function(){ return $(this).attr("rel").toLowerCase() == "stylesheet"; }) .filter(function(){ // this filter contributed by "mindinquiring" var media = $(this).attr("media"); return (media.toLowerCase() == "" || media.toLowerCase() == "print") }) .each(function(){ head += '<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" >'; }); head += "</head>"; return head; }
function getBody( printElement ) { return '<body><div class="' + $(printElement).attr("class") + '">' + $(printElement).html() + '</div></body>'; }
function getFormData( ele ) { $("input,select,textarea", ele).each(function(){ // In cases where radio, checkboxes and select elements are selected and deselected, and the print // button is pressed between select/deselect, the print screen shows incorrectly selected elements. // To ensure that the correct inputs are selected, when eventually printed, we must inspect each dom element var type = $(this).attr("type"); if ( type == "radio" || type == "checkbox" ) { if ( $(this).is(":not(:checked)") ) this.removeAttribute("checked"); else this.setAttribute( "checked", true ); } else if ( type == "text" ) this.setAttribute( "value", $(this).val() ); else if ( type == "select-multiple" || type == "select-one" ) $(this).find( "option" ).each( function() { if ( $(this).is(":not(:selected)") ) this.removeAttribute("selected"); else this.setAttribute( "selected", true ); }); else if ( type == "textarea" ) { var v = $(this).attr( "value" ); if ($.browser.mozilla) { if (this.firstChild) this.firstChild.textContent = v; else this.textContent = v; } else this.innerHTML = v; } }); return ele; }
function Iframe() { var frameId = settings.id; var iframeStyle = 'border:0;position:absolute;width:0px;height:0px;left:0px;top:0px;'; var iframe;
try { iframe = document.createElement('iframe'); document.body.appendChild(iframe); $(iframe).attr({ style: iframeStyle, id: frameId, src: "" }); iframe.doc = null; iframe.doc = iframe.contentDocument ? iframe.contentDocument : ( iframe.contentWindow ? iframe.contentWindow.document : iframe.document); } catch( e ) { throw e + ". iframes may not be supported in this browser."; }
if ( iframe.doc == null ) throw "Cannot find document.";
return iframe; }
function Popup() { var windowAttr = "location=yes,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no"; windowAttr += ",width=" + settings.popWd + ",height=" + settings.popHt; windowAttr += ",resizable=yes,screenX=" + settings.popX + ",screenY=" + settings.popY + ",personalbar=no,scrollbars=no";
var newWin = window.open( "", "_blank", windowAttr );
newWin.doc = newWin.document;
return newWin; } })(jQuery);