gpt4 book ai didi

javascript - 打印窗口第一次不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:44:03 30 4
gpt4 key购买 nike

我正在尝试使用打印选项保存 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 onload="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 似乎没有呈现内容。相反,我更改了 bodyinnerHTML,这似乎工作正常:

$("#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 onload="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();
});

http://jsfiddle.net/hoqp8zxp/

更新以帮助您处理 CSS

关于 CSS,我认为有几个问题:

  1. 新窗口位置是 about:blank(或类似的东西),因此相对路径将不起作用。您应该使用绝对路径(http://host/css/print.css 而不是 css/print.css)
  2. 由于样式表是异步加载的,您可能必须使用 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 onload="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 - 打印窗口第一次不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34023276/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com