我正在尝试在弹出窗口中打印fancybox的内容(它就像一个灯箱)并且遇到问题。原因是我直接调用print();
时没有加载css这是我的代码:
function printFancy() {
var $fancycontent = $("div.fancybox-overlay .fancybox-inner").clone();
$fancycontent.find(".dontprint").remove();
var title = $fancycontent .find(".title");
var base = $("base").attr("href");
var mywindow = window.open('', 'print', 'height='+$(window).height()+',width='+$(window).width());
mywindow.document.write('<html><head><title>'+title.html()+'</title>');
mywindow.document.write('<base href="'+base+'">');
$("link[rel=stylesheet]").each(function() {
if($(this).attr("href").indexOf("neededstyles")!=-1) {
var stylesheet=$(this).attr("href");
mywindow.document.write('<link rel="stylesheet" href="'+stylesheet+'" type="text/css" />');
}
});
//this dont get fired.
mywindow.document.write('</head><body onload="window.print()">');
mywindow.document.write($fancycontent.html());
mywindow.document.write('</body></html>');
//this dont get fired.
mywindow.onload = function() {
console.log("onload to print");
};
//this get fired but the CSS is not loaded
jQuery(mywindow.document).ready(function() {
console.log("ready to print");
// mywindow.print();
// mywindow.close();
});
}
正如你所看到的,我尝试了不同的可能性,但没有一种可行。我真的不明白为什么mywindow.onload
和身体onload根本不会被解雇。
我的理解是否有错误?为什么不起作用?或者是否有另一种更好的方法来打印fancybox(或任何其他div)的内容?
目前我正在使用:
setTimeout(function(){mywindow.print();mywindow.close()},1000);
哪个有效。如果有人连接速度很慢,不确定它是否会起作用。
编辑:在Firefox中,只有在我关闭弹出窗口时才会出现printdialog。必须有一个更好的解决方案。