7

我必须通过以下方式打印出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 和其他浏览器告诉我页面没有响应。为什么会这样?

4

4 回答 4

8

你必须做 amywindow.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());
  });
});
于 2012-08-02T16:53:29.957 回答
1

我看到您已经使用 mplungjan 关于窗口名称中没有空格的注释解决了它,但另一种方法是粗略地使用@media print 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>
于 2012-08-02T18:15:05.467 回答
1

就我而言,我只需要使用以下代码:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
于 2015-06-03T12:15:29.740 回答
0

在我的情况下,这个问题是通过在打印之前做一个焦点来解决的。

窗口.焦点();窗口.打印();

于 2014-12-12T20:01:20.083 回答