javascript 如何打印 iframe 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25682414/
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-28 04:50:14  来源:igfitidea点击:

how can I print iframe content

javascriptiframe

提问by Muhiddin Jumaniyazov

I want to print iframe content and I could not do it, I found the urlI'm a new promgrammer so i didn't understant the question and answers please explane me a few simple
thanks in advance

我想打印 iframe 内容,但我做不到,我找到了 url我是一个新的程序员,所以我没有理解问题和答案,请提前给我一些简单的
感谢

回答by Hotted24

Here it is :

这里是 :

Your iframe :

你的 iframe :

<iframe id="printf" name="printf"></iframe>

Printing :

印刷 :

window.frames["printf"].focus();
window.frames["printf"].contentWindow.print();

BTW, try to explain your problem in a better way next time (posting code can help) =)

顺便说一句,下次尝试以更好的方式解释您的问题(发布代码可以提供帮助)=)

Ref: Javascript Print iframe contents only

参考Javascript 仅打印 iframe 内容

EDIT :

编辑 :

As OP said didn't work, i'm referring the testpage which was linked in Ref:

正如 OP 所说没有用,我指的是参考文献中链接的测试页:

Print an iframe

打印 iframe

You can also try this function (same ref, different poster) :

你也可以试试这个功能(相同的参考,不同的海报):

//id is the  id of the iframe
function printFrame(id) {
            var frm = document.getElementById(id).contentWindow;
            frm.focus();// focus on contentWindow is needed on some ie versions
            frm.print();
            return false;
}