javascript - Window.print 在 IE 中不起作用

标签 javascript internet-explorer printing 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;
    }

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

编辑:我按以下方式调用 print:

$('#print_it').click(function(){
    var element = $('#itinerario');
    PrintElem(element);
});

这里 print_it 是按钮的 id。

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

最佳答案

您必须执行 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());
  });
});

关于javascript - Window.print 在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11782092/

相关文章:

javascript - 为什么我无法跳出这个 for 循环?

java - 3D java数组打印,另一种方式

javascript - 我想使用 javascript 向元素插入属性

javascript - 如何在范围索引 Angular js中传递动态值

javascript - 将具有两个属性的对象分组到表中

javascript - SetAttribute 在 IE 中不起作用

java - IE 9 设置 cookie 和重定向失败

css - IE 中的高度显示与 Firefox 不同

android - 如何使用 Wi-Fi Direct 连接在 Android 中通过 WiFi 打印机进行打印?

java - 一个链表打印,但另一个非空链表不打印 : Revised and expanded