【问题标题】:Unable to print an iframe on IE using JavaScript, prints parent page instead无法使用 JavaScript 在 IE 上打印 iframe,而是打印父页面
【发布时间】:2013-07-08 13:34:06
【问题描述】:

我有以下 JavaScript 代码:

$(document).ready(function() {
    $('a#print_button').click(function(event) {
        event.preventDefault();

        var print_url = 'print.html';

        if ($('#print_page').length == 0) {
            $("body").append('<iframe id="print_page" name="print_page" src=' + print_url + ' style="display: none; @media print { display: block; }"></iframe>');
        } else {
            $('#print_page').attr("src", print_quote_url);
        }

        $('#print_page').on("load", function() {
            frames["print_page"].focus();
            frames["print_page"].print();
        });
    });
});

它适用于 Chrome 和 Firefox。但是当我点击 IE 上的按钮时,它会打印父页面,而不是打印 iframe。

【问题讨论】:

    标签: javascript jquery internet-explorer printing focus


    【解决方案1】:

    我一直在寻找在 Internet Explorer 中打印 iframe 的解决方案。经过数小时的搜索,我得到了解决方案。它对我有用。请检查以下链接

    http://bytes.com/topic/misc/answers/629926-ie7-printing-iframe-solution

    document.execCommand('print', false, null);
    

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这在理论上可以回答问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
    • 非常感谢。我也花了很多时间来寻找它的 IE8 解决方案,它对我有用。
    • 这似乎在 Firefox 中不起作用。所以最好先检查一下浏览器厂商,然后IE才用这个。
    猜你喜欢
    • 2016-09-12
    • 2014-08-31
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2011-12-03
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多