从父窗口打印动态创建的iframe的内容 [英] Printing contents of a dynamically created iframe from parent window
问题描述
我有一个页面,其中包含一个链接列表和一个div,它用作链接指向的页面的占位符。每次用户点击链接时,都会在div中创建iframe,并且其src属性会获取链接href属性的值 - 简单明了,类似于外部网页库。
我遇到的问题是打印iframe的内容。要做到这一点,我使用此代码:
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();
}
问题似乎是iframe是由JQuery动态创建的 - 当我将iframe插入html代码时,浏览器可以打印外部页面。但是使用JavaScript注入相同的代码没有任何反应。我尝试在打印链接上使用JQ 1.3live事件,但没有成功。有没有办法克服这个问题?
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?
推荐答案
假设这就是你的iframe:
Suppose that this is what your iframe looks like:
<iframe id='print-iframe' name='print-frame-name'></iframe>
这是你使用jQuery的方法:
This is how you do it using jQuery:
$("#print-iframe").get(0).contentWindow.print();
这就是你使用原生JavaScript的方式:
and this is how you do it using native JavaScript:
document.getElementById("print-iframe").contentWindow.print();
这篇关于从父窗口打印动态创建的iframe的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!