javascript-打印窗口第一次不工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-打印窗口第一次不工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2929字,纯文字阅读大概需要5分钟。
内容图文
![javascript-打印窗口第一次不工作](/upload/InfoBanner/zyjiaocheng/664/d9deb0665fa847b7b9198783e1dd5add.jpg)
我正在尝试使用打印选项保存PDF,但是由于某种原因第一次无法使用它时,它带有一个空白页面.我有Googled,有一些解决方案,但是没有运气.
提前致谢
$("#btnPrint").click(function () {
// alert("hi");
$(".accordion-content").css('display','block');
var printWindow = window.open('', '', 'height=400,width=1200');
var strin="";
printWindow.document.write('<html><head><link rel="stylesheet" type="text/css" href="css/print.css"/></head><body onl oad="window.focus();" onbeforeunload="window.close();">');
// alert($(".logoHolder").html());
printWindow.document.write($(".logoHolder").html());
$('input[class="subCheck"]:checked').each(function() {
strin += $("#"+this.value).html()+"<br><br><br>";
});
printWindow.document.write(strin);
// alert();
printWindow.document.write('</body></html>');
printWindow.print();
printWindow.close();
//$( "#btnPrint" ).trigger( "click" );
});
解决方法:
为了检查问题出在哪里,我修改了代码,以便将所有内容放入变量中,然后将其写入窗口.在尝试打印窗口之前,document.write似乎没有呈现内容.相反,我更改了主体的innerHTML,似乎工作正常:
$("#btnPrint").click(function(){
$(".accordion-content").css('display','block');
var printWindow = window.open('', '', 'height=400,width=1200');
var html = ''+
'<html>'+
'<head>'+
'<link rel="stylesheet" type="text/css" href="css/print.css"/>'+
'</head>'+
'<body onl oad="window.focus();" onbeforeunload="window.close();">'+
$(".logoHolder").html();
$('input[class="subCheck"]:checked').each(function() {
html += $("#"+this.value).html()+"<br><br><br>";
});
html += '</body></html>';
//printWindow.document.write(html);
printWindow.document.body.innerHTML = html;
printWindow.print();
});
更新以帮助您使用CSS
关于CSS,我相信有两个问题:
>新窗口的位置约为:空白(或类似位置),因此相对路径将不起作用.您应该使用绝对路径(http://host/css/print.css而不是css / print.css)
>由于样式表是异步加载的,因此在样式表完全加载后,您可能必须使用hack才能执行打印命令.诸如此类:https://viget.com/inspire/js-201-run-a-function-when-a-stylesheet-finishes-loading注意:您也可以只使用样式标签加载样式
这是使用上述技巧(使用StackOverflow的样式表)更新的代码:
$("#btnPrint").click(function(){
$(".accordion-content").css('display','block');
var printWindow = window.open('', '', 'height=400,width=1200');
var cssFile = 'http://cdn.sstatic.net/stackoverflow/all.css?v=544053bd81fe';//Replace with your absolute path
var html = ''+
'<html>'+
'<head>'+
'<link rel="stylesheet" type="text/css" href="'+cssFile+'"/>'+
'</head>'+
'<body onl oad="window.focus();" onbeforeunload="window.close();">'+
$(".logoHolder").html();
$('input[class="subCheck"]:checked').each(function() {
html += $("#"+this.value).html()+"<br><br><br>";
});
html += '</body></html>';
//printWindow.document.write(html);
printWindow.document.body.innerHTML = html;
var img = document.createElement('img');
printWindow.document.body.appendChild(img);
img.style.display = 'none';
img.onerror = function(){
printWindow.print();
};
img.src = cssFile;
});
http://jsfiddle.net/hoqp8zxp/1/
内容总结
以上是互联网集市为您收集整理的javascript-打印窗口第一次不工作全部内容,希望文章能够帮你解决javascript-打印窗口第一次不工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。