Window.print并不在IE浏览器(Window.print doesn't work

2019-06-26 17:45发布

我必须打印出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的一起告诉我,该页面不响应。 这究竟是为什么?

Answer 1:

你必须做一个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());
  });
});


Answer 2:

我看你使用有关在窗口名中没有空格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>


Answer 3:

对我来说,我只需要使用下面的代码:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />


Answer 4:

在我的情况下,这个问题是由印刷之前做了重点解决。

window.focus()的; window.print();



文章来源: Window.print doesn't work in IE