Window.print在IE中不起作用

Jac*_*rky 7 javascript printing internet-explorer document

我必须以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;
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是在IE上,当我点击按钮时没有任何反应.但是,在Chrome和Firefox上它可以运行.我该怎么做才能正确打印出来?

编辑:我打电话print给以下方式:

$('#print_it').click(function(){
    var element = $('#itinerario');
    PrintElem(element);
});
Run Code Online (Sandbox Code Playgroud)

这是print_it按钮的id.

我看到的另一件事是,经过一段时间后,Chrome和其他浏览器告诉我该页面没有响应.为什么会这样?

mpl*_*jan 8

你必须做一个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;
}
Run Code Online (Sandbox Code Playgroud)

但我不确定window.close()是否会干扰打印

那么为何不

$(function() {
  $('#print_it').click(function(){
    popup($('#itinerario').html());
  });
});
Run Code Online (Sandbox Code Playgroud)

  • 我已经告诉过你了.没有空间.我的代码使用下划线 (2认同)