Window.print doesn't work in IE

后端 未结 4 2145
遥遥无期
遥遥无期 2020-12-19 06:21

I have to print out a div which I\'m doing in the following way:

function PrintElem(elem)
    {
        Popup(elem.html());
    }

    function          


        
相关标签:
4条回答
  • 2020-12-19 06:51

    On my case I just needed to use the code below:

    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
    
    0 讨论(0)
  • 2020-12-19 07:02

    You MUST do a mywindow.document.close() and you may NOT have a space in the window name

    I assume you invoke PrintElem from onclick of something - if that something is a link, you need to return false in the onclick handler!

    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

    And why not

    $(function() {
      $('#print_it').click(function(){
        popup($('#itinerario').html());
      });
    });
    
    0 讨论(0)
  • 2020-12-19 07:09

    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:

    <!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>
    
    0 讨论(0)
  • 2020-12-19 07:13

    In my case this issue was resolved by doing a focus before the print.

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

    0 讨论(0)
提交回复
热议问题