Window.print在IE中不起作用 [英] Window.print doesn't work in IE

查看:183
本文介绍了Window.print在IE中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须以下列方式打印出 div

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

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上它可以运行。我该怎么做才能正确打印出来?

My problem is that on IE, when I click the button nothing happens. However, on Chrome and Firefox it works. What can I do to print it out correctly?

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

I'm call print in the following way:

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

这是 print_it 的id按钮。

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

Another thing I've seen is that after a period of time, Chrome along with other browsers tells me that the page isn't responding. Why is this happening?

推荐答案

你必须做一个 mywindow.document.close()你可以窗口名称中没有空格

我假设你从onclick中调用PrintElem - 如果那样的话是一个链接,你需要在onclick处理程序中返回false!

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

如果我不得不

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()是否会干扰打印

but I am not sure whether or not the window.close() will interfere with the printing

为什么不

$(function() {
  $('#print_it').click(function(){
    popup($('#itinerario').html());
  });
});

这篇关于Window.print在IE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆