javascript Window.print 在 IE 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11782092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Window.print doesn't work in IE
提问by HymanTurky
I have to print out a div
which I'm doing in the following way:
我必须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;
}
My problem is that on IE, when I click the button nothing happens. However, on Chrome and Firefox it works. What can I do to print it out correctly?
我的问题是在 IE 上,当我单击按钮时什么也没有发生。但是,在 Chrome 和 Firefox 上它可以工作。我该怎么做才能正确打印出来?
EDIT:I'm call print
in the following way:
编辑:我print
通过以下方式调用:
$('#print_it').click(function(){
var element = $('#itinerario');
PrintElem(element);
});
This is where print_it
is the id of the button.
这是print_it
按钮的ID。
Another thing I've seen is that after a period of time, Chrome along with other browsers tells me that the page isn't responding. Why is this happening?
我看到的另一件事是,经过一段时间后,Chrome 和其他浏览器告诉我该页面没有响应。为什么会这样?
回答by mplungjan
You MUST do a mywindow.document.close()
and you may NOT have a space in the window name
您必须执行 amywindow.document.close()
并且窗口名称中不能有空格
I assume you invoke PrintElem from onclick of something - if that something is a link, you need to return false in the onclick handler!
我假设您从某个东西的 onclick 调用 PrintElem - 如果那个东西是一个链接,您需要在 onclick 处理程序中返回 false!
Here is how I would do it if I had to
如果我必须这样做,我会这样做
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;
}
but I am not sure whether or not the window.close() will interfere with the printing
但我不确定 window.close() 是否会干扰打印
And why not
那么为何不
$(function() {
$('#print_it').click(function(){
popup($('#itinerario').html());
});
});
回答by Luis Sérgio
On my case I just needed to use the code below:
在我的情况下,我只需要使用下面的代码:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
回答by Andrew Morton
I see you've solved it using mplungjan's note about no spaces in the window name, but an alternative would be to use @media print css styles, crudely:
我看到你已经使用 mplungjan 关于窗口名称中没有空格的注释解决了这个问题,但另一种方法是粗略地使用 @media 打印 css 样式:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
@media print {
* {
visibility: hidden;
}
.printMe {
visibility: visible;
}
}
</style>
<script type="text/javascript" src="jquery-1.4.4.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#Button1").click(function () {
var divToPrint = $("#div3");
$(divToPrint).addClass("printMe");
window.print();
$(divToPrint).removeClass("printMe");
}
)
}
);
</script>
</head>
<body>
<div id="div1">fhgjghajghhjagjdag</div>
<div id="div2">ytiyrtiyertuyeyiyt</div>
<div id="div3">We want to print this one.</div>
<div id="div4">vnbcxbvmzxbvc,xbv</div>
<div id="buttonContainer">
<input id="Button1" type="button" value="button" />
</div>
</body>
</html>
回答by Anthony Griggs
In my case this issue was resolved by doing a focus before the print.
在我的情况下,这个问题是通过在打印前进行焦点解决的。
window.focus(); window.print();
window.focus(); 窗口。打印();