如何通过JavaScript实现打印网页特定区域?
- 内容介绍
- 文章标签
- 相关推荐
本文共计788个文字,预计阅读时间需要4分钟。
javascript// 打印指定页面部分PrintArea('PrintArea');
js 打印部分页面 PrintAreaPrintArea /** * 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: Print * Print Area : ... html ... * Javascript : * 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() + "" + PrintArea.getHead() + PrintArea.getBody( $ele ) + "" ); PADocument.close(); }, docType : function() { if ( settings.mode == modes.iframe ) return ""; if ( settings.standard == standards.html5 ) return ""; var transitional = settings.standard == standards.loose ? " Transitional" : ""; var dtd = settings.standard == standards.loose ? "loose" : "strict"; return ''; }, getHead : function() { var extraHead = ""; var links = ""; if ( settings.extraHead ) settings.extraHead.replace( /([^,]+)/g, function(m){ extraHead += m }); $(document).find("link") .filter(function(){ // Requirement: 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 += ' '; }); if ( settings.extraCss ) settings.extraCss.replace( /([^,\s]+)/g, function(m){ links += ' ' }); return "
本文共计788个文字,预计阅读时间需要4分钟。
javascript// 打印指定页面部分PrintArea('PrintArea');
js 打印部分页面 PrintAreaPrintArea /** * 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: Print * Print Area : ... html ... * Javascript : * 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() + "" + PrintArea.getHead() + PrintArea.getBody( $ele ) + "" ); PADocument.close(); }, docType : function() { if ( settings.mode == modes.iframe ) return ""; if ( settings.standard == standards.html5 ) return ""; var transitional = settings.standard == standards.loose ? " Transitional" : ""; var dtd = settings.standard == standards.loose ? "loose" : "strict"; return ''; }, getHead : function() { var extraHead = ""; var links = ""; if ( settings.extraHead ) settings.extraHead.replace( /([^,]+)/g, function(m){ extraHead += m }); $(document).find("link") .filter(function(){ // Requirement: 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 += ' '; }); if ( settings.extraCss ) settings.extraCss.replace( /([^,\s]+)/g, function(m){ links += ' ' }); return "

