jQuery之浏览器打印插件

1、jQuery.print.js打印插件

地址:https://github.com/DoersGuild/jQuery.print

该打印插件适用于局部打印

基础使用方法

$(selector).print();

或者

$("#myElementId").print({
        	globalStyles: true,
        	mediaPrint: false,
        	stylesheet: null,
        	noPrintSelector: ".no-print",
        	iframe: true,
        	append: null,
        	prepend: null,
        	manuallyCopyFormValues: true,
        	deferred: $.Deferred(),
        	timeout: 750,
        	title: null,
        	doctype: '<!doctype html>'
	});

参数说明

参数名称类型默认值说明备注
globalStylesbooleantrue是否应包括父文档中的样式

1.设置为true时,打印预览样式为自定义设置的print样式,比如:

    <style media="print">
        @page{
            size:A4 landscape;/*打印纸张 A4 横向打印*/
            margin:0mm;  /*纸张页边距*/
        }
    </style>

mediaPrintbooleanfalse是否包含media='print'的链接标签。会被globalStyles选项覆盖 
stylesheetstringnull外部样式表的URL地址 
noPrintSelectorstring".no-print"不想打印的元素(css样式或者某个元素) 
iframebooleantrue是否弹出预览打印窗口 
appendstringnull在打印的元素内部结尾添加html文本或者或者某个元素相当于$(selector).append(content)
prependstringnull在打印的元素内部开头添加html文本或者或者某个元素相当于$(selector).prepend(content)
manuallyCopyFormValuesbooleantrue打印时是否显示form/input表单 
deferred jQuery.Deferred$.Deferred()调用print函数后解析的延迟对象 
timeoutnumber750打印预览窗口弹出的延迟时间单位:毫秒
titlestringnull更改打印标题 
doctypestring'<!doctype html>'有效的doctype字符串 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

兼容的jQuery版本

  • jQuery v. 1.7.2
  • jQuery v. 1.9.1
  • jQuery v. 2.2.0
  • jQuery v. 3.1.1

兼容的浏览器

  • Google Chrome - v 20, 26, 48, 55
  • Internet Explorer - v 10, 11
  • Firefox - v 35

注:如果打印的内容,包含table且跨页显示,table表头会重复出现

2、printThis.js插件

地址:https://github.com/jasonday/printThis

该打印插件适用于局部打印

基础使用方法

$(selector).printThis()

或者

$("#mySelector").printThis({
    debug: false,               // show the iframe for debugging
    importCSS: true,            // import parent page css
    importStyle: false,         // import style tags
    printContainer: true,       // print outer container/$.selector
    loadCSS: "",                // path to additional css file - use an array [] for multiple
    pageTitle: "",              // add title to print page
    removeInline: false,        // remove inline styles from print elements
    removeInlineSelector: "*",  // custom selectors to filter inline styles. removeInline must be true
    printDelay: 333,            // variable print delay
    header: null,               // prefix to html
    footer: null,               // postfix to html
    base: false,                // preserve the BASE tag or accept a string for the URL
    formValues: true,           // preserve input/form values
    canvas: false,              // copy canvas content
    doctypeString: '...',       // enter a different doctype for older markup
    removeScripts: false,       // remove script tags from print content
    copyTagClasses: false,      // copy classes from the html & body tag
    beforePrintEvent: null,     // function for printEvent in iframe
    beforePrint: null,          // function called before iframe is filled
    afterPrint: null            // function called before iframe is removed
});

 参数说明

参数名类型默认值说明备注
debugbooleanfalsedebug时是否显示打印预览弹窗 
importCSSbooleantrue是否导入父级页面样式 
importStylebooleanfalse是否使用默认设置的打印样式

1.设置为true时,打印预览样式为自定义设置的print样式,比如:

    <style media="print">
        @page{
            size:A4 landscape;/*打印纸张 A4 横向打印*/
            margin:0mm;  /*纸张页边距*/
        }
    </style>

printContainerbooleantrue打印时是否包含选择元素的最外层 
loadCSSstring/arrary" "添加外部css样式文件地址 - 多个使用 array []数组 
pageTitlestring" "打印页面添加标题 
removeInlinebooleanfalse删除打印元素的内联样式如果form表单中input元素border原来设置为none,设置为true时;border的none样式无效
removeInlineSelectorstring"*"用于筛选内联样式的自定义选择器. removeInline 必须为true 才有效PS:removeInline 设置为true时,设置选择器后,打印没看出太大变化
printDelaynumber333打印(打印预览弹窗显示)延迟时间 单位:毫秒
headerstringnull在打印的元素内部开头添加html文本相当于$(selector).prepend(content)
footerstringnull在打印的元素内部结尾添加html文本相当于$(selector).append(content)
basebooleanfalse保留基本标记或接受URL的字符串 
formValuesbooleantrue打印时是否显示 input/form 表单 
canvasbooleanfalse是否复制canvas内容 
doctypeStringstring'<!DOCTYPE html>'设置 doctype 替换原有的doctype标记如果输入值不是有效的doctype标记,那么会在首页的左上角显示设置的字符串,比如为doctype,那么首页的左上角显示会显示doctype一串文字
removeScriptsbooleanfalse是否从打印内容中删除script脚本标记 
copyTagClassesbooleanfalse是否从 html 和 body 复制样式 
beforePrintEventfunctionnull打印预览弹窗中打印事件需要执行的事件方法如果此处设置了 function函数,那么在打印预览弹窗弹出后,预览显示会很卡,不建议使用,并且测试发现 在关闭关闭打印预览弹窗后也会执行该方法
beforePrintfunctionnull打印预览弹窗弹出之前需要执行的事件方法 
afterPrintfunctionnull打印完成或者点击取消关闭打印预览弹窗后需要执行的事件方法 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

兼容的jQuery版本

  • jQuery v. 1.7.2
  • jQuery v. 1.9.1
  • jQuery v. 2.2.0
  • jQuery v. 3.1.1

兼容的浏览器版本

  • Google Chrome - v 20, 26, 48, 55
  • Internet Explorer - v 10, 11
  • Firefox - v 35

注:打印元素可以选择多个,比如

$('#kitty-one, #kitty-two, #kitty-three').printThis({
    importCSS: false,
    loadCSS: "path/to/new/CSS/file",
    header: "<h1>Look at all of my kitties!</h1>"
});

 

3、jquery.print-preview.js

地址:https://github.com/etimbo/jquery-print-preview-plugin

该打印插件适用于整个页面打印和局部打印

该插件使用比较简单,请参照地址中的demo

 

jQuery打印插件 jQuery.print是一个用于打印页面特定部分的插件 用法 导入jQuery后将其包含在HTML中,如: < script type = “ text / JavaScript ” src = “ path / to / jquery.print.js ” > </ script > 使用它像: $(“#myElementId ”)。print(/ * options * /); 要么 $。print(“#myElementId ” / *,options * /); 您可以提交选项对象,如: $(“#myElementId ”)。print({ globalStyles : 是的, mediaPrint : false, stylesheet : null, noPrintSelector : “。 no -print ”, iframe : 是的, append : null, 前置: null, manualCopyFormValues : true, 延期: $。延期(), 超时: 750, title : null, doctype : ' <!doctype html> ' }); 目前,此插件支持以下选项: globalStyles 默认: true 可接受的值:布尔值 功能:是否应包含父文档中的样式 mediaPrint 默认: false 可接受的值:布尔值 功能:是否应包含带有media ='print'的链接标签; 由globalStyles选项覆盖 样式表 默认: null 可接受的值:URL字符串 功能:要包括的外部样式表的URL noPrintSelector 默认: ".no-print" 可接受的值:任何有效 jQuery-selector 功能:要从打印中排除的项目的选择器 IFRAME 默认值:true,如果传递no-vaild iframe选择器,则创建隐藏的iframe 可接受的值:任何有效jQuery-selector或布尔值 功能:是否从iframe打印而不是弹出窗口; 可以将jQuery-selector现有iframe作为值 附加/添加 默认: null 可接受的值:任何有效jQuery-selector或HTML文本 功能:在选定内容之前(前置)或之后(追加)添加自定义HTML manuallyCopyFormValues 默认: true 可接受的值:布尔值 功能:是否应将用户更新的表单输入值复制到打印的标记上(这可以通过手动迭代每个表单元素来完成) 延期的 默认: $.Deferred() 可接受的值:任何有效的jQuery.Deferred对象 功能:一旦调用print函数就解析的jQuery.Deferred对象。可用于设置回调 - 请参阅wiki 超时 默认: 750 可接受的值:以毫秒为单位的时间 setTimeout 功能:在创建新窗口/ iframe之前更改等待内容等加载内容的最大时间量,如果新窗口/ iframe 的load事件尚未触发,则作为后备 标题 默认值:null,使用主页标题 可接受的值:任何单行字符串 功能:更改打印的标题 DOCTYPE 默认: '<!doctype html>' Acceptable-Values:任何有效的doctype字符串 功能:将doctype添加到打印的文档框架中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值