无法使用 JavaScript 在 IE 上打印 iframe,而是打印父页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17573733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:50:01  来源:igfitidea点击:

Unable to print an iframe on IE using JavaScript, prints parent page instead

javascriptjqueryinternet-explorerprintingfocus

提问by Debiprasad

I have the following JavaScript code:

我有以下 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();
        });
    });
});

It works on Chrome and Firefox. But when I click on the button on IE, it prints the parent page, instead of printing the iframe.

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

回答by sriram

I was looking for a solution to print Iframes in Internet Explorer. After hours of search i got the solution. it worked for me. Please check the following link

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

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

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

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