js中的打印
来源:互联网 发布:淘宝抢月饼 编辑:程序博客网 时间:2024/06/02 01:44
1,js文件(jquery.PrintArea.js)
*首选引用此js文件
/** * Version 2.4.0 Copyright (C) 2013 * Tested in IE 11, FF 28.0 and Chrome 33.0.1750.154 * No official support for other browsers, but will TRY to accommodate challenges in other browsers. * Example: * Print Button: <div id="print_button">Print</div> * Print Area : <div class="PrintArea" id="MyId" class="MyClass"> ... html ... </div> * Javascript : <script> * $("div#print_button").click(function(){ * $("div.PrintArea").printArea( [OPTIONS] ); * }); * </script> * options are passed as json (example: {mode: "popup", popClose: false}) * * {OPTIONS} | [type] | (default), values | Explanation * --------- | --------- | ---------------------- | ----------- * @mode | [string] | (iframe),popup | printable window is either iframe or browser popup * @popHt | [number] | (500) | popup window height * @popWd | [number] | (400) | popup window width * @popX | [number] | (500) | popup window screen X position * @popY | [number] | (500) | popup window screen Y position * @popTitle | [string] | ('') | popup window title element * @popClose | [boolean] | (false),true | popup window close after printing * @extraCss | [string] | ('') | comma separated list of extra css to include * @retainAttr | [string[]] | ["id","class","style"] | string array of attributes to retain for the containment area. (ie: id, style, class) * @standard | [string] | strict, loose, (html5) | Only for popup. For html 4.01, strict or loose document standard, or html 5 standard * @extraHead | [string] | ('') | comma separated list of extra elements to be appended to the head tag */(function($) { var counter = 0; var modes = { iframe : "iframe", popup : "popup" }; var standards = { strict : "strict", loose : "loose", html5 : "html5" }; var defaults = { mode : modes.iframe, standard : standards.html5, popHt : 500, popWd : 400, popX : 200, popY : 200, popTitle : '', popClose : false, extraCss : '', extraHead : '', retainAttr : ["id","class","style"] }; var settings = {};//global settings $.fn.printArea = function( options ) { $.extend( settings, defaults, options ); counter++; var idPrefix = "printArea_"; $( "[id^=" + idPrefix + "]" ).remove(); settings.id = idPrefix + counter; var $printSource = $(this); var PrintAreaWindow = PrintArea.getPrintWindow(); PrintArea.write( PrintAreaWindow.doc, $printSource ); setTimeout( function () { PrintArea.print( PrintAreaWindow ); }, 1000 ); }; var PrintArea = { print : function( PAWindow ) { var paWindow = PAWindow.win; $(PAWindow.doc).ready(function(){ paWindow.focus(); paWindow.print(); if ( settings.mode == modes.popup && settings.popClose ) setTimeout(function() { paWindow.close(); }, 2000); }); }, write : function ( PADocument, $ele ) { PADocument.open(); PADocument.write( PrintArea.docType() + "<html>" + PrintArea.getHead() + PrintArea.getBody( $ele ) + "</html>" ); PADocument.close(); }, docType : function() { if ( settings.mode == modes.iframe ) return ""; if ( settings.standard == standards.html5 ) return "<!DOCTYPE html>"; var transitional = settings.standard == standards.loose ? " Transitional" : ""; var dtd = settings.standard == standards.loose ? "loose" : "strict"; return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01' + transitional + '//EN" "http://www.w3.org/TR/html4/' + dtd + '.dtd">'; }, getHead : function() { var extraHead = ""; var links = ""; if ( settings.extraHead ) settings.extraHead.replace( /([^,]+)/g, function(m){ extraHead += m }); $(document).find("link") .filter(function(){ // Requirement: <link> element MUST have rel="stylesheet" to be considered in print document var relAttr = $(this).attr("rel"); return ($.type(relAttr) === 'undefined') == false && relAttr.toLowerCase() == 'stylesheet'; }) .filter(function(){ // Include if media is undefined, empty, print or all var mediaAttr = $(this).attr("media"); return $.type(mediaAttr) === 'undefined' || mediaAttr == "" || mediaAttr.toLowerCase() == 'print' || mediaAttr.toLowerCase() == 'all' }) .each(function(){ links += '<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" >'; }); if ( settings.extraCss ) settings.extraCss.replace( /([^,\s]+)/g, function(m){ links += '<link type="text/css" rel="stylesheet" href="' + m + '">' }); return "<head><title>" + settings.popTitle + "</title>" + extraHead + links + "</head>"; }, getBody : function ( elements ) { var htm = ""; var attrs = settings.retainAttr; elements.each(function() { var ele = PrintArea.getFormData( $(this) ); var attributes = "" for ( var x = 0; x < attrs.length; x++ ) { var eleAttr = $(ele).attr( attrs[x] ); if ( eleAttr ) attributes += (attributes.length > 0 ? " ":"") + attrs[x] + "='" + eleAttr + "'"; } htm += '<div ' + attributes + '>' + $(ele).html() + '</div>'; }); return "<body>" + htm + "</body>"; }, getFormData : function ( ele ) { var copy = ele.clone(); var copiedInputs = $("input,select,textarea", copy); $("input,select,textarea", ele).each(function( i ){ var typeInput = $(this).attr("type"); if ($.type(typeInput) === 'undefined') typeInput = $(this).is("select") ? "select" : $(this).is("textarea") ? "textarea" : ""; var copiedInput = copiedInputs.eq( i ); if ( typeInput == "radio" || typeInput == "checkbox" ) copiedInput.attr( "checked", $(this).is(":checked") ); else if ( typeInput == "text" ) copiedInput.attr( "value", $(this).val() ); else if ( typeInput == "select" ) $(this).find( "option" ).each( function( i ) { if ( $(this).is(":selected") ) $("option", copiedInput).eq( i ).attr( "selected", true ); }); else if ( typeInput == "textarea" ) copiedInput.text( $(this).val() ); }); return copy; }, getPrintWindow : function () { switch ( settings.mode ) { case modes.iframe : var f = new PrintArea.Iframe(); return { win : f.contentWindow || f, doc : f.doc }; case modes.popup : var p = new PrintArea.Popup(); return { win : p, doc : p.doc }; } }, Iframe : function () { var frameId = settings.id; var iframeStyle = 'border:0;position:absolute;width:0px;height:0px;right:0px;top:0px;'; var iframe; try { iframe = document.createElement('iframe'); document.body.appendChild(iframe); $(iframe).attr({ style: iframeStyle, id: frameId, src: "#" + new Date().getTime() }); 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; }, Popup : function () { 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=yes"; var newWin = window.open( "", "_blank", windowAttr ); newWin.doc = newWin.document; return newWin; } };})(jQuery);
2.jsp页面
<div class="modal fade bs-example-modal-lg" id="printDialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <div style="float: right" id="close"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true" id="closeButton"> × </button> </div> <h5 class="modal-title"><span>状态: </span><span class="productStatus"></span></h5> </div> <div class="modal-body"> <div style="float: left" id="productTitle"> <h4 class="text-align-center">产品配置清单</h4> </div> <table id="OrderAbstract" class="<%--table table-bordered--%> table table-bordered table-hover table-striped"> <thead> </thead> <tbody> <tr> <td>产品名称:</td> <td><span class="productType"></span></td> <td>产品型号:</td> <td><span class="productSpecName"></span></td> </tr> <tr> <td>云桌面类型:</td> <td><span class="productVdesktopType"></span></td> <td>云桌面用途:</td> <td><span class="productPurpose"></span></td> </tr> <tr> <td>操作系统:</td> <td><span class="productImage"></span></td> <td>申请台数:</td> <td><span class="applyNum"></span></td> </tr> <tr> <td style="border: none">申请人:</td> <td><span id="user"></span></td> <td>配置人:</td> <td><span id="configureUser"></span></td> </tr> <tr> <td>单位名称:</td> <td><span id="org"></span></td> <td>单位名称:</td> <td><span id="configureOrg"></span></td> </tr> <tr> <td>申请时间:</td> <td><span id="createTime"></span></td> <td>配置时间:</td> <td><span id="configureTime"></span></td> </tr> </tbody> </table> </div> </div> <div class="modal-footer" id="cancleLog"> <button type="button" class="btn btn-primary" id="printLog"> 打印 </button> <button type="button" class="btn btn-primary" data-dismiss="modal" aria-hidden="true"> 返回 </button> </div> </div> </div><!-- /.modal-dialog --></div>
3,js执行以下方法即可
$("#printDialog").printArea();
阅读全文
0 0
- JS中的打印功能
- js中的打印
- js的window.print打印页面 不打印网页中的“打印”按钮
- js 打印,打印预览
- js 打印
- js打印
- js打印
- JS 打印
- js打印
- JS 打印
- js打印
- 打印JS
- js打印
- js打印
- JS 打印
- Js打印
- js打印
- JS打印
- SphereFace python抽取人脸特征
- mac 终端中添加tree命令显示文件目录结构
- 关于C++中STL的一些东西(不定期整理)
- 小学奥数思维训练题(十三)
- 数据的初步认知
- js中的打印
- logback的配置和使用
- window.showmodaldialog的使用
- 【Scikit-Learn 中文文档】处理文本数据
- @RequestBody使用
- jena解析关于基因的go.owl文件(正在学习中)
- 说说canvas吧(一):paint
- Python 使用 pandas 进行数据分析
- VGGFace python 抽取人脸特征