Javascript 通过JQuery打印DIV内容
假设我的页面中有许多div,但我想使用jquery打印特定div的内容。我发现有一个插件 使用此插件,我们可以轻松打印div内容,如下所示Javascript 通过JQuery打印DIV内容,javascript,jquery,Javascript,Jquery,假设我的页面中有许多div,但我想使用jquery打印特定div的内容。我发现有一个插件 使用此插件,我们可以轻松打印div内容,如下所示 $('SelectorToPrint').printElement(); 但如果我的div被隐藏在页面中会发生什么呢。那么它就行了。这个插件可以打印隐藏div的内容吗 如果打印机未与客户端计算机连接,会发生什么情况。若打印机不存在,我想向客户显示消息“打印机未找到”?如何处理这种情况。所以,请告诉我什么是最好的方法来打印页面中隐藏的div的内容,以及处理
$('SelectorToPrint').printElement();
但如果我的div被隐藏在页面中会发生什么呢。那么它就行了。这个插件可以打印隐藏div的内容吗
如果打印机未与客户端计算机连接,会发生什么情况。若打印机不存在,我想向客户显示消息“打印机未找到”?如何处理这种情况。所以,请告诉我什么是最好的方法来打印页面中隐藏的div的内容,以及处理打印机问题,如果打印机没有连接
谢谢你的问题听起来不像是你自己试过的,所以我甚至不想回答,但是:如果一个隐藏的div不能用那一行打印出来:
$('SelectorToPrint').printElement();
只需将其更改为:
$('SelectorToPrint').show().printElement();
这将使它在所有情况下都能工作
对于其他人来说,没有解决办法。该插件将为您打开打印对话框,用户必须在其中选择打印机。您根本无法确定打印机是否连接了javascript(如果您正在考虑的话,您(几乎)无法在没有打印对话框的情况下打印)
注:
jQuery的1.9.x版中删除了$.browser
对象
使此库不受支持
有一种方法可以将其用于隐藏的div,但是您需要更多地使用printElement()函数和css Css: 脚本:
$("#SelectorToPrint").printElement({ printBodyOptions:{styleToAdd:'padding:10px;margin:10px;display:block', classNameToAdd:'WhatYouWant'}})
这将覆盖显示:在您打开的新窗口中没有显示,内容将显示在打印预览页面上,并且站点上的div将保持隐藏状态。尝试此jquery库,jquery打印元素
我更喜欢这一款,我已经测试过它及其工作原理 或
您可以按照以下步骤操作:
$('SelectorToPrint').printElement()代码>
这里是一个JQuery&JavaScript解决方案,用于按div样式打印div(使用内部和外部css)
$(document).ready(function() {
$("#btnPrint").live("click", function () {//$btnPrint is button which will trigger print
var divContents = $(".order_summery").html();//div which have to print
var printWindow = window.open('', '', 'height=700,width=900');
printWindow.document.write('<html><head><title></title>');
printWindow.document.write('<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" >');//external styles
printWindow.document.write('<link rel="stylesheet" href="/css/custom.css" type="text/css"/>');
printWindow.document.write('</head><body>');
printWindow.document.write(divContents);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.onload=function(){
printWindow.focus();
printWindow.print();
printWindow.close();
}
});
});
$(文档).ready(函数(){
$(“#btnPrint”).live(“单击”,函数(){/$btnPrint是将触发打印的按钮
var divContents=$(“.order_summy”).html();//必须打印的div
变量printWindow=window.open('',''高度=700,宽度=900');
printWindow.document.write(“”);
printWindow.document.write(“”);//外部样式
printWindow.document.write(“”);
printWindow.document.write(“”);
printWindow.document.write(divContents);
printWindow.document.write(“”);
printWindow.document.close();
printWindow.onload=函数(){
printWindow.focus();
printWindow.print();
printWindow.close();
}
});
});
这将在新窗口中打印您的div
触发事件的按钮
<input type="button" id="btnPrint" value="Print This">
你试过使用它吗?隐藏的div没有使用插件打印这不起作用。printWindow.close();最后关闭第二个窗口,其中有一个打印对话框。
$("#mySelector").printThis({
* debug: false, * show the iframe for debugging
* importCSS: true, * import page CSS
* printContainer: true, * grab outer container as well as the contents of the selector
* loadCSS: "path/to/my.css", * path to additional css file
* pageTitle: "", * add title to print page
* removeInline: false * remove all inline styles from print elements
* });
$(document).ready(function() {
$("#btnPrint").live("click", function () {//$btnPrint is button which will trigger print
var divContents = $(".order_summery").html();//div which have to print
var printWindow = window.open('', '', 'height=700,width=900');
printWindow.document.write('<html><head><title></title>');
printWindow.document.write('<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" >');//external styles
printWindow.document.write('<link rel="stylesheet" href="/css/custom.css" type="text/css"/>');
printWindow.document.write('</head><body>');
printWindow.document.write(divContents);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.onload=function(){
printWindow.focus();
printWindow.print();
printWindow.close();
}
});
});
<input type="button" id="btnPrint" value="Print This">