在主页上,我尝试打印一个弹出窗口。在 Chrome 上它可以工作,但在 Firefox 中它不起作用,不知何故它忽略了 css 文件。
在Firefox中,窗口正在加载某些内容,但我不知道它是什么,因为我无法打开Firebug。格式设置HTML看起来正确,但打印机未获取css信息。所以我认为可能是,因为他加载了一些东西。
HTML+JS
代码(小提琴不允许document.write
):
http://pastebin.com/QXASsTXg
popup.css
:
http://pastebin.com/HM6JEMnX
很抱歉粘贴,但代码对于堆栈溢出来说太大了。
Javascript Code
:
$( document ).ready(function() {
$('.depart span').click(function(){
//close all popups
$('.popup').hide();
//open current popup
$(this).next().slideDown();
});
$('.close').click(function(){
// close all popups
$('.popup').slideUp();
});
$('span.printbutton').click(function(){
printPopup($(this));
});
});
function printPopup(data){
var popup = data.parent().parent().parent('div.popup');
mywindow = window.open('', 'my div', 'height=600,width=420');
mywindow.document.write('<html><head><title>Popup</title>');
mywindow.document.write('<link rel="stylesheet" media="screen" href="popup.css" type="text/css" />');
mywindow.document.write('<link rel="stylesheet" media="print" href="popup.css" type="text/css" />');
mywindow.document.write('</head>')
mywindow.document.write('<body class="popupwindow">')
mywindow.document.write('<div class="popup">')
mywindow.document.write(popup.html());
mywindow.document.write('</div>')
mywindow.document.write('</body></html>');
// mywindow.location.reload();
mywindow.focus();
mywindow.print();
setTimeout('mywindow.close();', 5000);
return true;
}
希望有人能帮忙。
编辑:发现一个新的提示,当弹出窗口加载循环时,我必须按 ESC 键取消循环,然后我可以按 STRG+P 并且它工作正常。错误可能在哪里?
我自己修复了它,如果其他人有同样的问题,这是解决方案:在你最后一次之后
mywindow.document.write('</html>');
对于 Firefox,您需要以下行:
mywindow.document.close();
在那行之后,Firefox停止等待其他内容,我可以毫无问题地打印弹出窗口。不知何故,Chrome 忽略了缺失的行,我工作正常,但 Firefox 需要它才能正常工作。
Throurg Jquery Pull Css from That Printout Page
INSTEAD OF :
mywindow.document.write('<link rel="stylesheet" media="screen" href="popup.css" type="text/css" />');
mywindow.document.write('<link rel="stylesheet" media="print" href="popup.css" type="text/css" />');
USE : $("#printoutpage").css();
See If It Works
尝试引用 css 文件的绝对路径:
mywindow.document.write('<link rel="stylesheet" media="screen" href="pathtocsspopup.css" type="text/css" />');
mywindow.document.write('<link rel="stylesheet" media="print" href="pathtocsspopup.css" type="text/css" />');