我必须按以下方式打印出一个 div
:
function PrintElem(elem)
{
Popup(elem.html());
}
function Popup(data)
{
var mywindow = window.open('', 'to print', 'height=600,width=800');
mywindow.document.write('<html><head><title></title>');
mywindow.document.write('<link rel="stylesheet" href="css/mycss.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.print();
mywindow.close();
return true;
}
我的问题是,在 IE 上,当我单击按钮时没有任何反应。但是,在 Chrome 和 Firefox 上它可以工作。我该怎么做才能正确打印出来?
编辑:我按以下方式调用 print
:
$('#print_it').click(function(){
var element = $('#itinerario');
PrintElem(element);
});
这里 print_it
是按钮的 id。
我看到的另一件事是,一段时间后,Chrome 和其他浏览器告诉我页面没有响应。为什么会这样?
最佳答案
您必须执行 mywindow.document.close()
并且您可以窗口名称中不能有空格
我假设您从某些东西的 onclick 调用 PrintElem - 如果那东西是一个链接,您需要在 onclick 处理程序中返回 false!
如果有必要,我会这样做
function PrintElem(elem) {
Popup($(elem).html());
}
function Popup(data)
{
var mywindow = window.open('', 'to_print', 'height=600,width=800');
var html = '<html><head><title></title>'+
'<link rel="stylesheet" href="css/mycss.css" type="text/css" />'+
'</head><body onload="window.focus(); window.print(); window.close()">'+
data+
'</body></html>';
mywindow.document.write(html);
mywindow.document.close();
return true;
}
但我不确定 window.close() 是否会干扰打印
为什么不
$(function() {
$('#print_it').click(function(){
popup($('#itinerario').html());
});
});
关于javascript - Window.print 在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11782092/