从父窗口打印动态创建的 iframe 的内容 [英] Printing contents of a dynamically created iframe from parent window
问题描述
我有一个页面,其中包含一个链接列表和一个 div,该 div 用作链接指向的页面的占位符.每次用户单击链接时,都会在 div 中创建一个 iframe,其 src 属性获取链接 href 属性的值 - 简单明了,类似于外部网页图库.我确实有问题是打印 iframe 的内容.为此,我使用以下代码:
<前>函数 PrintIframe(){框架[名称"].focus();框架[名称"].print();}问题似乎是 iframe 是由 JQuery 动态创建的 - 当我将 iframe 正确插入到 html 代码中时,浏览器可以正常打印外部页面.但是使用 JavaScript 注入的相同代码没有任何反应.我尝试在打印"链接上使用 JQ 1.3 'live' 事件,但没有成功.有没有办法克服这个问题?
假设你的 iframe 是这样的:
这就是你使用 jQuery 的方式:
$("#print-iframe").get(0).contentWindow.print();
这就是使用原生 JavaScript 的方式:
document.getElementById("print-iframe").contentWindow.print();
I have a page with a list of links and a div which serves as a placeholder for the pages the links lead to. Everytime user clicks on a link an iframe is created in the div and its src attribute gets the value of the link href attribute - simple and clear, sort of external web pages gallery. What i do have a problem with is printing the contents of the iframe. To do this i use this code:
function PrintIframe() { frames["name"].focus(); frames["name"].print(); }
The issue seems to be that iframe is created dynamically by JQuery - when I insert an iframe right into the html code, the browser prints the external page all right. But with the same code injected by JavaScript nothing happens. I tried to use JQ 1.3 'live' event on "Print" link, no success. Is there a way to overcome this?
Suppose that this is what your iframe looks like:
<iframe id='print-iframe' name='print-frame-name'></iframe>
This is how you do it using jQuery:
$("#print-iframe").get(0).contentWindow.print();
and this is how you do it using native JavaScript:
document.getElementById("print-iframe").contentWindow.print();
这篇关于从父窗口打印动态创建的 iframe 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!